C#Do While循环问题

本文关键字:问题 循环 While C#Do | 更新日期: 2023-09-27 18:24:29

我是编码新手,我真的被这个C#边做边循环的业务卡住了。经过很长一段时间的处理,我终于让它循环一个语句,但现在它只是重复最初的语句。例如,如果它生成数字6,而你猜7,它会说"太高……",但当你再次猜测你是太低还是等于6时,它仍然会说"过高……"

感谢

    static void Main(string[] args)
    {
        Random r = new Random();
        int intRandomNum;
        // ask the random num gen for num between 1 and 12
        intRandomNum = r.Next(10) + 1;
        //ask user for their first guess
        Console.WriteLine("I'm going to generate a random number, can you guess what it is? " + intRandomNum);
        int intUserNum = int.Parse(Console.ReadLine());
        do
        {
            if (intUserNum == intRandomNum)
            {
                Console.WriteLine("You got it! Great job!");
            }
            if (intUserNum < intRandomNum)
            {
                Console.WriteLine("Too low! Try Again.");
                Console.ReadLine();
            }
            if (intUserNum > intRandomNum)
            {
                Console.WriteLine("Too high! Try again.");
                Console.ReadLine();
            }
        } while (intUserNum != intRandomNum);
        Console.ReadKey();
    }
}

}

感谢大家的帮助!我明白了!终于

C#Do While循环问题

您需要将解析放入循环中,以便重新计算intUserNum

    int intUserNum = int.Parse(Console.ReadLine());
    do
    {
        if (intUserNum == intRandomNum)
        {
            Console.WriteLine("You got it! Great job!");
        }
        if (intUserNum < intRandomNum)
        {
            Console.WriteLine("Too low! Try Again.");
            intUserNum = int.Parse(Console.ReadLine());
        }
        if (intUserNum > intRandomNum)
        {
            Console.WriteLine("Too high! Try again.");
            intUserNum = int.Parse(Console.ReadLine());
        }
    } while (intUserNum != intRandomNum);

编写时,您的代码从用户那里读取新行,但它从不将其转换为数字,因此您的变量永远不会更改。这会导致你的第一个错误猜测被永远使用。

请注意,考虑使用int.TryParse而不是int.Parse可能也是一个好主意。这允许您处理用户是否键入了非整数的内容。。。

这是因为实际将用户的数字提取到intUserNum中的代码,即

int intUserNum = int.Parse(Console.ReadLine());

在循环的外部,因此它只执行一次。每次循环重复时,它都会检查相同的旧数字。

编码提示:请记住,计算机不知道您打算做什么。您的意图可能是正确的,即intUserNum应该始终包含用户猜测的下一个数字。然而,计算机只执行你告诉它的操作,并且你告诉它用户只应该输入一次数字——在循环中,你永远不会说intUserNum应该具有新值,因此它将永远保留旧值。

在循环内移动输入读数:

int intUserNum;
    do
    {
        intUserNum = int.Parse(Console.ReadLine());
        if (intUserNum == intRandomNum)
        {
            Console.WriteLine("You got it! Great job!");
        }
        if (intUserNum < intRandomNum)
        {
            Console.WriteLine("Too low! Try Again.");
        }
        if (intUserNum > intRandomNum)
        {
            Console.WriteLine("Too high! Try again.");
        }
    } while (intUserNum != intRandomNum);
int intUserNum = int.Parse(Console.ReadLine());

应该在循环的顶部。

不知道C#,但应该知道。

您必须在循环中获取并解析输入。否则,值始终保持不变:

// ...
do
{
    int intUserNum = int.Parse(Console.ReadLine());
    if (intUserNum == intRandomNum)
        Console.WriteLine("You got it! Great job!");
    if (intUserNum < intRandomNum)
    {
        Console.WriteLine("Too low! Try Again.");
        Console.ReadLine();
    }
    if (intUserNum > intRandomNum)
    {
        Console.WriteLine("Too high! Try again.");
        Console.ReadLine();
    }
} while (intUserNum != intRandomNum);
static void Main(string[] args)
{
    var rnd = new Random();
    var n = rnd.Next(13);
    while (!Guess(n)) ;
    Console.ReadKey();
}
static bool Guess(int n)
{
    int input;
    if (!int.TryParse(Console.ReadLine(), out input))
        return false;
    var msg = input == n ? "Win" : input < n ? "Low" : "High";
    Console.WriteLine(msg);
    return input == n;
}

我自己也遇到了类似的问题,在几乎两节课上都很吃力,直到我(讽刺地)在输入自己的问题时找到了你的答案。

{
    class Program
    {
        static void Main(string[] args)
        {
            string aKnight = "Romulus";
            string bKnight = "Remus";
            string Weapon;

            Console.WriteLine($"{aKnight} implies {bKnight} is unorthodox.");
            /*Console.ReadLine();*/
            Console.WriteLine($"{bKnight} deicdes to hit {aKnight}. What should he use? Club, Bow, or Tactical Nuke?");
            /*Console.ReadLine();*/

            do
            {
                Weapon = Console.ReadLine();
                switch (Weapon)
                {
                    case "Club":
                        Console.WriteLine("Works for me.");
                        Console.WriteLine($"{bKnight} knocks {aKnight} out with the club.");
                        Console.ReadLine();
                        break;
                    case "Bow":
                        Console.WriteLine("Eh, creative yet acceptable.");
                        Console.WriteLine($"{bKnight} hits {aKnight} over the head with the bow. It just hurt a little.");
                        Console.ReadLine();
                        break;
                    case "Tactical Nuke":
                        Console.WriteLine("Are.. Are you serious? For the love of... JUST WHY?!");
                        Console.WriteLine("TACTICAL NUKE INBOUND!!!... It lagged...");
                        Console.ReadLine();
                        break;
                    default:
                        Console.Clear();
                        Console.WriteLine("Please make a choice.");
                        //Console.ReadLine();
                        break;
                }
            } while (Weapon != default && Weapon != "Club" && Weapon != "Bow" && Weapon != "Tactical Nuke");
                Console.WriteLine("What will happen next ?");
                Console.ReadLine();
        }
    }
}

我所需要做的就是将Weapon=Console.ReadLine()移到do/while括号中,并在括号中添加(&&Weapon!="Club"&武器!="Bow"&武器!="Tactical Nuke")。非常感谢!:D