using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace sample
{
class Program
{
static void Main(string[] args)
{
int[] studentNumber = new int[10];
string firstname, lastname;
int i, unit;
double grade, gwa;
Console.Write("enter first name: ");
firstname = Console.ReadLine();
Console.Write("enter last name: ");
lastname = Console.ReadLine();
Console.Write("enter student number: ");
if (studentNumber.Length != 10)
{
Console.Write("student number must be 10 characters!");
Console.ReadLine();
}
MessageBox.Show(firstname + " " + lastname + "\n" + studentNumber, "Mapua");
}
}
}
How can I check if the user entered exactly 10 characters? And if the user did, how would I call that to my message box method? How could I show that in the output? Thank you!
You can just check with,
if(studentNumber.Length != 10)
{
Console.Write("Student number must be 10 characters");
}
And you cannot use messageBox in a console application, change it to windows form.
Here's one way to get the student number from the user, forcing them to enter 10 digits, and adding each valid character to your studentNumber array. The code basically does this:
Prompt the user to input the student number
While they have input less than 10 numbers:
Read the next key entered by the user
If the key entered is not a number (which we can determine by ensuring that the character is not less than 0 or greater than 9), then subtract one from the cursor postion (making them re-enter the number), write a blank space over their entry, and back up the cursor position again (so it looks like their input was rejected, which it was).
If the key is a number, add it to our array and increment a counter so we know when they've entered 10 numbers:
Here's the sample code:
int[]studentNumber = new int[10];
Console.Write("enter student number: ");
int numbersEntered = 0;
while (numbersEntered < 10)
{
var input = Console.ReadKey();
// If the input is not a number, then reject their input by
// backing up one character, writing a blank space over their
// entry, and then back up again so we can get the next key.
if (input.KeyChar < '0' || input.KeyChar > '9')
{
Console.CursorLeft -= 1;
Console.Write(" ");
Console.CursorLeft -= 1;
continue;
}
studentNumber[numbersEntered] = int.Parse(input.KeyChar.ToString());
numbersEntered++;
}
At the end of this loop, the studentNumber array will contain the 10 numbers entered by the user.
Related
I am trying to make an app where you can add phone numbers and other stuff and I want to check if the number is in correct format so i have been using this loop to check this
int numer = int.Parse(Console.ReadLine());
int count = 0;
int number = numer;
while (number > 0)
{
number /= 10;
count++;
if (number)
{
Console.WriteLine(count);
stringList.Add(nazwa);
intList.Add(numer);
//Console.Clear();
Console.WriteLine($"You added new contact {nazwa} which number is {numer} ");
Console.WriteLine();
}
else
{
Console.WriteLine(count);
//Console.Clear();
Console.WriteLine("This number is in a wrong format!");
Console.WriteLine();
}
}
The problem is when i will type number that has 10 digits program will add this number to the database and after that is going to send error to the user.
To validate it you just need:
string input = Console.ReadLine();
bool valid = input.Length == 10 && input.All(char.IsDigit);
I just saw that you want to validate phone-numbers. Well, then this is not sufficient. But if a phone number is valid depends on the country/region. Search for phone number regex(for example in US).
namespace A3PFJBJLP1
{
class Program
{
static void Main(string[] args)
{
// option 1 by parker farewell
Console.WriteLine("~ option 1 ~");
Console.WriteLine("please input a number: ");
for (int StartingNum = Convert.ToInt32(Console.ReadLine()); StartingNum < 20; StartingNum++)
{
Console.WriteLine(StartingNum);
}
Console.ReadLine();
}
}
}
So far this is the code I've tried and I can display the numbers in a list, but only if the number is 20 or less when I need to make a list that displays 20 whole numbers that come after the number inputted by the user
You were close. If you start at a number userInput, you want to display 20 numbers after it you could update the loop as follows.
int userInput = Convert.ToInt32(Console.ReadLine()); // get and store user input
for (int startingNum = userInput; startingNum < userInput + 20; startingNum++)
{
Console.WriteLine(startingNum + 1); // display number after startingNum
}
This question already has answers here:
What's the main difference between int.Parse() and Convert.ToInt32
(13 answers)
Closed 4 years ago.
This is the program. Can someone tell me what I did wrong? Every time I input a value for the string SolvingFor, the program crashes. I'm new to coding so if I made a stupid mistake please tell me.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PercentageCalculator
{
class Program
{
static void Main(string[] args)
{
string SolvingFor;
int part;
int whole;
int percent;
Console.WriteLine("please only use numbers and lowercase letters.");
Console.WriteLine("Are you trying to solve for percent, part, or whole?");
SolvingFor = Convert.ToString(Console.Read());
if (SolvingFor == "part") {
Console.WriteLine("Please Enter Value of the Whole");
whole = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please Enter Value of the Percent");
percent = Convert.ToInt32(Console.ReadLine()); ;
Console.WriteLine("Your answer is" + (whole * percent) / 100); }
else if (SolvingFor == "whole")
{
Console.WriteLine("Please Enter Value of the part");
part = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please Enter Value of the Percent");
percent = Convert.ToInt32(Console.ReadLine()); ;
Console.WriteLine("Your answer is" + (part * 100) / percent);
}
else if (SolvingFor == "percent")
{
Console.WriteLine("Please Enter Value of the part");
part = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please Enter Value of the whole");
whole = Convert.ToInt32(Console.ReadLine()); ;
Console.WriteLine("Your answer is" + (part * 100) / whole);
}
else
{
Console.WriteLine("Please only input valid lowercase letters and numbers. ");
};
Console.Read();
}
}
}
//SolvingFor = Convert.ToString(Console.Read());
SolvingFor = Convert.ToString(Console.ReadLine());
if you put(hover) the mouse over ReadLine
you will see that it returns a string so Convert.ToString is not needed
SolvingFor = Console.ReadLine();
First Issue:
Below line from your code is reading just one character.
Instead of Console.Read(), use Console.ReadLine() to enable you to input multiple characters.
SolvingFor = Convert.ToString(Console.Read());
Second Issue:
You are using Convert.ToInt32(Console.Readline());
Please note that Convert.ToInt32 will throw an exception in anything apart from number is provided as input.
Better use int.TryParse which returns if the conversion is successful or not.
I'm making a where the users enters as many numbers as they want,so long as they're between 1 and 10, then once they're done the program lists the number they entered and how many times. Like this
Now my question is how would you make C# read and check which numbers they entered and print them out and how many times they entered it like in the bottom of the picture? Sorry for the noob question i'm new to this language.
Here is what I have so far:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Assignment_3
{
class Program
{
static void Exercise1()
{
//int min;
//int max;
//Console.WriteLine("Enter minimum integer: ");
//min = int.Parse(Console.ReadLine());
//Console.WriteLine("Enter maximum integer: ");
//max = int.Parse(Console.ReadLine());
//int start = min;
}
static void Exercise2()
{
string entry;
Console.Write("Enter an integer or 'q' to quit ");
entry = Console.ReadLine();
while (entry.ToLower() != "q")
{
int number;
if (int.TryParse(entry, out number))
{
if (number >= 1 && number <= 10)
{
}
else
{
Console.WriteLine("Your number must be between 1 and 10");
}
}
Console.Write("Enter an integer or 'q' to quit ");
entry = Console.ReadLine();
}
}
static void Main(string[] args)
{
Exercise1();
Exercise2();
Console.WriteLine("Press enter to end");
Console.ReadLine();
}
}
}
Well, we do not do your homework, so I will just point you around in the documentation with enough information you are not lost.
Use a Dictionary to keep the nnumber and counter. This is easiest - if a number is entered, get the counter, add one, put back into dictionary. If not - set 1 as value for the number.
At the end you go throuch all the keys (that are the numbers) and print the corresponding values (the number of times).
I made a program that asks you to enter a few numbers less than 100, and then takes the numbers you entered and tells you which ones were valid entries. What I want to add is a feature that takes those valid entries and finds the smallest and largest numbers. After it finds the smallest and largest values I want them to be displayed under where it says "Invalid entries:." Can anyone help me with adding this?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Programming_Exercise_2_Chapter_6
{
class Program
{
static void Main(string[] args)
{
string answer;
do
{
Console.Clear();
Header();
int number;
string indata;
List<int> validEntries = new List<int>();
List<string> invalidEntries = new List<string>();
while (true)
{
Console.WriteLine("Insert numbers less than 100: ");
indata = Console.ReadLine();
if (Int32.TryParse(indata, out number))
{
if (number <= 100 && number > 0)
validEntries.Add(number);
else
invalidEntries.Add(number.ToString());
}
else
invalidEntries.Add(indata);
Console.WriteLine("Press N to stop. Press enter to continue.");
indata = Console.ReadLine();
Console.Clear();
if (indata == "n"|| indata == "N")
break;
}
DisplayEntries(validEntries, invalidEntries);
Console.ReadLine();
Console.WriteLine("Do you want to try again?(Enter Y for Yes, or N for No)");
answer = Console.ReadLine();
}
while (answer == "Y" || answer == "y");
}
static void DisplayEntries(List<int> validEntries, List<string> invalidEntries)
{
Console.WriteLine("Your valid entries were: ");
foreach (int i in validEntries)
Console.WriteLine(i);
Console.WriteLine();
Console.WriteLine("Your invalid entries were: ");
foreach (string s in invalidEntries)
Console.WriteLine(s);
}
static void Header()
{
Console.WriteLine("\tNumber Validation App");
Console.WriteLine("Please enter a few numbers less than 100.\nValid entries will be displayed.");
Console.WriteLine("");
}
}
}
If I understood your question, you want to have the smallest and largest entry from your integer list?
In that case you can simply sort the list and retrieve the first / last entry of the sorted list:
validEntries.Sort();
var smallest = validEntries.First();
var highest = validEntries.Last();
Is that what you were looking for?
So you have a list of integers(numbers). ranging between 1 and 100. all of the stored in validEntries.
So you have to go through all numbers in the list, when you find a high one you store it and compare it to the next number in the list.
int highest_nr = 0;
int lowest_nr = 100;
foreach (int i in validEntries)
{
if (i < lowest_nr)
lowest_nr = i
if (i > highest_nr)
highest_nr = i;
Console.WriteLine(i);
}
Console.WriteLine("Highest number = " + highest_nr.ToString());
Console.WriteLine("Lowest number = " + lowest_nr.ToString());
validEntries.Max() and validEntries.Min() will get you your highest and lowest values, respectively.
Console.WriteLine("Your valid entries were: ");
foreach (int i in validEntries)
Console.WriteLine(i);
Console.WriteLine();
Console.WriteLine("Your invalid entries were: ");
foreach (string s in invalidEntries)
Console.WriteLine(s);
Would be written (I replaced your foreach):
Console.WriteLine("Your valid entries were: ");
Console.WriteLine(string.Join(Environment.NewLine, validEntries));
Console.WriteLine();
Console.WriteLine("Your invalid entries were: ");
Console.WriteLine(string.Join(Environment.NewLine, invalidEntries));
Console.WriteLine();
Console.WriteLine("Min:{0} Max:{1}",validEntries.Min(),validEntries.Max());