Hi I'm practicing the "Switch Loop" in a program. And I'm making a code where a user can input the integer and after the user will input the integer it will also display what the user just typed in. Now I'm trying to implement where the program will ask the user to input the number again by selecting the Y/N. I already included it here in my code but if I type characters the first time the program asks me to type an integer, the program will execute the catch part. How can I make it that if the user will type a character it will also display the message again, "please enter the integer:"
Thanks for your help I'm quite lost in somewhere here.
int enterYourNumber;
char shortLetter;
try
{
Console.WriteLine("Please enter the integer: ");
enterYourNumber = Convert.ToInt32(Console.ReadLine());
WriteNumber(enterYourNumber);
Console.WriteLine("Do you still want to enter a number? Y/N");
shortLetter = Convert.ToChar(Console.ReadLine());
while (shortLetter == 'y' || shortLetter == 'Y')
{
Console.WriteLine("Please enter the integer: ");
enterYourNumber = Convert.ToInt32(Console.ReadLine());
WriteNumber(enterYourNumber);
Console.WriteLine("Do you still want to enter a number? Y/N");
shortLetter = Convert.ToChar(Console.ReadLine());
}
}
catch
{
Console.WriteLine("Please enter an integer not a character");
}
}
public static void WriteNumber(int wordValue)
{
switch (wordValue)
{
case 1:
Console.WriteLine("You have entered number one");
break;
case 2:
Console.WriteLine("You have entered number two");
break;
case 3:
Console.WriteLine("You have entered number three");
break;
default:
Console.WriteLine("You have exceeded the range of number 1-3 ");
break;
}
======================
This is what I did I don't know why I'm getting an error. The new method seems not to work:
int enterYourNumber;
char shortLetter;
do
{
enterYourNumber = GetNumber();
WriteNumber(enterYourNumber);
Console.WriteLine("Do you still want to enter a number? Y/N");
shortLetter = Convert.ToChar(Console.ReadLine());
}
while (shortLetter == 'y' || shortLetter == 'Y')
{
Console.WriteLine("Please enter the integer: ");
enterYourNumber = Convert.ToInt32(Console.ReadLine());
WriteNumber(enterYourNumber);
Console.WriteLine("Do you still want to enter a number? Y/N");
shortLetter = Convert.ToChar(Console.ReadLine());
}
}
public static int GetNumber()
{
bool done = false;
int value;
while ( !done )
{
Console.WriteLine("Please enter the integer: ");
try
{
value = Convert.ToInt32(Console.ReadLine());
done = true;
}
catch
{
Console.WriteLine("Please enter an integer not a character");
}
}
}
=============
Hi Bill this is the example you gave and it seems I'm still getting an error: could you please the code. Thank you very much:
public static void Main(string[] args)
{
int enterYourNumber;
char shortLetter;
do
{
enteryourNumber = GetNumber();
WriteNumber(enteryourNumber);
Console.WriteLine("Do you still want to enter a number? Y/N");
shortLetter = Convert.ToChar(Console.ReadLine());
}
while (shortLetter == 'y' || shortLetter == 'Y');
}
public static int GetNumber()
{
bool done = false;
int value;
while (!done)
{
Console.WriteLine("Please enter the integer: ");
try
{
value = Convert.ToInt32(Console.ReadLine());
done = true;
}
catch
{
Console.WriteLine("Please enter an integer not a character");
}
Console.WriteLine("Please enter the integer: ");
enterYourNumber = Convert.ToInt32(Console.ReadLine());
WriteNumber(enterYourNumber);
Console.WriteLine("Do you still want to enter a number? Y/N");
shortLetter = Convert.ToChar(Console.ReadLine());
}
}
public static void WriteNumber(int wordValue)
{
switch (wordValue)
{
case 1:
Console.WriteLine("You have entered number one");
break;
case 2:
Console.WriteLine("You have entered number two");
break;
case 3:
Console.WriteLine("You have entered number three");
break;
default:
Console.WriteLine("You have exceeded the range of number 1-3 ");
break;
}
}
}
}
=================
Hi again this question is for Robert. This is what I did now, but if I enter "N" it will not exit the program. It still asked the same question. Any idea: Thanks:
public static void Main(string[] args)
{
int enterYourNumber;
char shortLetter;
bool validEntry;
while (true)
{
do
{
Console.WriteLine("Please enter an integer: ");
string numberString = Console.ReadLine();
validEntry = int.TryParse(numberString, out enterYourNumber);
WriteNumber(enterYourNumber);
if (!validEntry)
{
Console.WriteLine("Entry must be an integer");
}
} while (!validEntry);
Console.WriteLine("Do you still want to enter a number? Y/N");
shortLetter = Convert.ToChar(Console.ReadLine());
}
}
public static void WriteNumber(int wordValue)
{
switch (wordValue)
{
case 1:
Console.WriteLine("You have entered number one");
break;
case 2:
Console.WriteLine("You have entered numbered two");
break;
case 3:
Console.WriteLine("You have entered numbered three");
break;
default:
Console.WriteLine("You have exceeded the range of number 1-3");
break;
}
}
}
}
====================================
Hi Robert and Bill here is what I got now. Would appreciate if you could give some ideas on how to improve the coding. Thanks for your help.
public static void Main(string[] args)
{
int intEnterYourNumber;
char charShortLetter;
string strUserInput;
do
{
do
{
Console.WriteLine("Please enter the integer: ");
strUserInput = Console.ReadLine();
} while (!int.TryParse(strUserInput, out intEnterYourNumber));
WriteNumber(intEnterYourNumber);
Console.WriteLine("Do you still want to enter a number? Y/N");
charShortLetter = Convert.ToChar(Console.ReadLine().ToUpper());
} while (charShortLetter == 'Y');
}
public static void WriteNumber(int wordValue)
{
switch (wordValue)
{
case 1:
Console.WriteLine("You have entered number one");
break;
case 2:
Console.WriteLine("You have entered numbered two");
break;
case 3:
Console.WriteLine("You have entered numbered three");
break;
default:
Console.WriteLine("You have exceeded the range of number 1-3");
break;
}
}