I seem to having a problem where I need an integer from a loop for the loop condition, here's the code:
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);
I need the choiceInt for the condition of the loop, to get this to work at the moment i have to let it loop through once before it gets a value,
You have your >
and <
wrong: you want a condition that evaluates to true
when the choice is wrong (hence the loop should continue asking the user for input). Since do
/ while
exits when the condition turns false
, you can be sure that after exiting the loop choiceInt
is in the valid range.
The condition should look like this:
do {
...
} (choiceInt < 0 || choiceInt > 10);
// ^^^^ ^^^^
// negative above ten
This will work for you:
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!"); }
I would be inclined to try an tidy up the code to make it more generic. Try doing this:
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));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.