我正在练习程序中的“Switch Loop”。我正在编写一个用户可以输入整数的代码,在用户输入整数后,它还会显示用户输入的内容。现在我正在尝试实现程序要求用户输入的位置通过选择Y / N再次编号。
我已将它包含在我的代码中但是如果我在第一次输入字符时要求我输入一个整数,程序将执行catch部分。我怎样才能说明如果用户输入一个字符,它也会再次显示该消息,“请输入整数:”
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;
}
这就是我所做的;我不知道为什么我会收到错误。新方法似乎不起作用:
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");
}
}
}
比尔,这是你给出的例子,看来我仍然得到一个错误:
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;
}
}
}
}
这个回复是罗伯特。这就是我现在所做的,但是如果我输入“N”,它将不会退出程序。它仍然问同样的问题。
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;
}
}
}
}
罗伯特和比尔,这就是我现在所拥有的。我怎样才能改善这个?
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;
}
}
将try / catch块移动到循环内。 Robert Harvey对do / while循环的建议也很好。
int enterYourNumber;
char shortLetter;
do
{
try
{
Console.WriteLine("Please enter the integer: ");
enterYourNumber = Convert.ToInt32(Console.ReadLine());
WriteNumber(enterYourNumber);
}
catch( FormatException )
{
Console.WriteLine("Please enter an integer not a character");
}
shortLetter = '\0';
do
{
try
{
Console.WriteLine("Do you still want to enter a number? Y/N");
shortLetter = Convert.ToChar(Console.ReadLine());
}
catch( FormatException )
{
Console.WriteLine("Please enter a single character");
}
}
while ( shortLetter == '\0' )
}
while (shortLetter == 'y' || shortLetter == '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 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;
}
我认为如果您没有使用例外来测试您的数字,您的代码将更容易学习和理解。尝试使用int.TryParse()代替(MSDN上的TryParse())。 TryParse()返回true(或false),具体取决于输入的数字是否有效:
int number;
bool validEntry = int.TryParse(enterYourNumber, out number);
if (!validEntry)
{
Console.WriteLine("Entry must be an integer.");
}
另外,了解do ... while循环(link)。 do..while循环类似于常规while循环,除了在评估条件表达式之前执行一次do-while循环。
bool validEntry;
int enteredNumber;
do
{
Console.Write("Please enter the integer: ");
string numberString = Console.ReadLine();
validEntry = int.TryParse(numberString, out enteredNumber);
if (!validEntry)
{
Console.WriteLine("Entry must be an integer.");
}
} while (!validEntry);
在循环中将整个东西包裹在另一个循环中(“你还想输入一个数字(Y / N)吗?”)并且你已经完成了。
do
{
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')
罗伯特是一个开始,但也许我们仍然想尝试抓住......
将主要功能替换为:
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() {
boolean 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");
}
}
}
然后在main函数中删除try catch。
由于您正在学习,您可能还会考虑以不同方式处理错误。 看看SO问题:In C# should try-catch be used for is-numeric testing?获取更多想法和讨论。