C# C语言中的循环故障#

C# C语言中的循环故障#,c#,C#,我似乎遇到了一个问题,我需要一个循环中的整数作为循环条件,下面是代码: 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 Kin

我似乎遇到了一个问题,我需要一个循环中的整数作为循环条件,下面是代码:

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);
do{
Console.WriteLine();
WriteLine(“您想测试什么文件?”);
控制台。WriteLine(“1.皇家同花顺”);
控制台。写入线(“2.直齐”);
Console.WriteLine(“3.4个一类”);
控制台。书写线(“4.座无虚席”);
控制台。写入线(“5.刷新”);
控制台。写线(“6.直”);
Console.WriteLine(“7.3个一类”);
控制台。写入线(“8.两对”);
控制台写入线(“9.对”);
控制台写入线(“10.退出”);
choiceInt=Convert.ToInt32(Console.ReadLine());
}而(choiceInt<10 | | choiceInt>0);

我需要循环条件的选择,为了让它在得到一个值之前循环一次,我必须让它运行,

你有你的
10);
//    ^^^^              ^^^^
//十点以上为负数

这将适用于您:

    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!"); }

我倾向于尝试整理代码,使其更通用。尝试这样做:

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));

为什么需要有一个循环?为什么不打印每个选项,然后读取并使用
开关
对值进行处理。它循环从文件中测试扑克手,这只是代码的一部分,我在代码下面有一些开关用于其他事情,但我只是发布了需要帮助的代码。无关,但也可以查看int.TryParse()-它将使您免于Convert.ToInt32()异常。我是否可以确认您正在尝试对从
1
9
的任何值重复循环,但在任何其他值上退出,但在
10
上显式退出?您提供了10作为选项,因此即使用户选择10,您的循环也会继续。@Nikita-否,当您输入
10
时,循环退出。我在发布之前测试了代码。你也检查了1到9吗?当choice=10(由用户输入)选项时,选项数组包含1到10。Any()条件返回true!options.Any()将为false,它将退出。但是,对于1到9,其退出的原因与上述相同。这不仅是错误的条件,而且我的第一条评论在您修改时仍然有效。@Nikita-我已经检查过了。对于所有正确的输入值(
1
10
),它将正确退出。对于所有其他的值,它都会循环。我想你误解了这个问题。它应该显示选项&让用户选择,直到用户退出。因此,对于1到9,它应该运行循环;对于10,它应该退出循环。@JasonJarrett当前代码在输入
10
时退出循环,因为将
10
赋值给
choiceInt
会将继续条件变为
false
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));