我似乎遇到一个问题,我需要循环条件的循环中的整数,这里是代码:
do {
Console.WriteLine();
Console.WriteLine("What file would you like to test?");
Console.WriteLine("1. Royal Flush");
Console.WriteLine("2. Straight Flush");
Console.WriteLine("3. Four of a Kind");
Console.WriteLine("4. Full House");
Console.WriteLine("5. Flush");
Console.WriteLine("6. Straight");
Console.WriteLine("7. Three of a Kind");
Console.WriteLine("8. Two Pair");
Console.WriteLine("9. Pair");
Console.WriteLine("10. Exit");
choiceInt = Convert.ToInt32(Console.ReadLine());
} while (choiceInt < 10 || choiceInt > 0);
我需要使用choiceInt作为循环的条件,为了让它在我获得一个值之前让它循环一次,
答案 0 :(得分:1)
您的>
和<
错误:当选择错误时,您需要一个评估为true
的条件(因此循环应继续询问用户输入)。由于do
/ while
在条件变为false
时退出,因此您可以确保在退出循环后choiceInt
处于有效范围内。
条件应如下所示:
do {
...
} (choiceInt < 0 || choiceInt > 10);
// ^^^^ ^^^^
// negative above ten
答案 1 :(得分:-1)
这对你有用:
static void Main(string[] args)
{
string consoleInput;
ShowOptions();
do
{
consoleInput = Console.ReadLine();
if (consoleInput == "10")
Environment.Exit(0);
DoSomething();
ShowOptions();
} while (consoleInput != null && consoleInput != "10");
}
private static void ShowOptions()
{
Console.WriteLine();
Console.WriteLine("What file would you like to test?");
Console.WriteLine("1. Royal Flush");
Console.WriteLine("2. Straight Flush");
Console.WriteLine("3. Four of a Kind");
Console.WriteLine("4. Full House");
Console.WriteLine("5. Flush");
Console.WriteLine("6. Straight");
Console.WriteLine("7. Three of a Kind");
Console.WriteLine("8. Two Pair");
Console.WriteLine("9. Pair");
Console.WriteLine("10. Exit");
}
private static void DoSomething() { Console.WriteLine("I am doing something!"); }
答案 2 :(得分:-1)
我倾向于尝试整理代码以使其更通用。试着这样做:
var options = new []
{
new { option = 1, text = "Royal Flush" },
new { option = 2, text = "Straight Flush" },
new { option = 3, text = "Four of a Kind" },
new { option = 4, text = "Full House" },
new { option = 5, text = "Flush" },
new { option = 6, text = "Straight" },
new { option = 7, text = "Three of a Kind" },
new { option = 8, text = "Two Pair" },
new { option = 9, text = "Pair" },
new { option = 10, text = "Exit" },
};
string choice;
do
{
Console.WriteLine();
Console.WriteLine("What file would you like to test?");
Console.WriteLine(
String.Join(
Environment.NewLine,
options.Select(o => String.Format("{0}. {1}", o.option, o.text))));
choice = Console.ReadLine();
} while (options.Take(9).Any(o => o.option.ToString() == choice));
答案 3 :(得分:-2)
你不必改变太多。只需将其设为do while循环而不是do循环。此外,你的while循环的条件似乎有点偏。如果值不在1-10范围内,你不想继续循环吗?
int choiceInt = Convert.ToInt32(Console.ReadLine());
while (choiceInt > 10 || choiceInt < 0) {
Console.WriteLine("What file would you like to test?");
Console.WriteLine("1. Royal Flush");
Console.WriteLine("2. Straight Flush");
Console.WriteLine("3. Four of a Kind");
Console.WriteLine("4. Full House");
Console.WriteLine("5. Flush");
Console.WriteLine("6. Straight");
Console.WriteLine("7. Three of a Kind");
Console.WriteLine("8. Two Pair");
Console.WriteLine("9. Pair");
Console.WriteLine("10. Exit");
choiceInt = Convert.ToInt32(Console.ReadLine());
}