嗨,我正在練習程序中的「開關循環」。我正在創建一個代碼,用戶可以輸入整數,在用戶輸入整數後,它也會顯示用戶輸入的內容。現在我試圖在程序要求用戶輸入通過選擇Y/N再次輸入號碼。我已經將它包含在我的代碼中,但如果我在程序第一次輸入字符時鍵入一個整數,程序將執行catch部分。我怎樣才能讓它,如果用戶將鍵入一個字符,它也將顯示消息,「請輸入整數:」switch語句
感謝您的幫助我很迷茫在這裏的某個地方。
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;
}
}
感謝羅伯特怎麼樣,如果用戶將輸入的第一次一個角色? – tintincutes 2009-06-14 01:16:14
把這個做/做在你的try/catch中,就像在原始代碼中一樣。 – 2009-06-14 01:24:15
不幸的是,它並沒有讓用戶在發生異常後回到循環中,這是一個問題。 – billjamesdev 2009-06-14 02:16:00