gpt4 book ai didi

c# - c# 中的数字猜谜游戏

转载 作者:太空宇宙 更新时间:2023-11-03 19:41:33 24 4
gpt4 key购买 nike

我正在 Visual Studio 中研究有关 C# 中二进制搜索的内容。我的计算机项目找到用户的猜测号码。所以,我主要使用 tihs 代码;

        int min = 0;    // minimum number in the array
int max = 100; // maximum number in the array
int middle = 50; // middle number in the array
int counter = 1;
string name, input;
int guess_number;

Console.WriteLine("Hello, this is a game that finding the number of in your mind. If you want to play so let me know you! ");
name = Console.ReadLine();
Console.WriteLine("Awesome welcome to the game " + name + " guess a number between " + min + " and " + max + " Please! ");
Console.WriteLine("Is your guess " + middle + " ?\nIf it's your guess then write (0) please!\nIf it's too high then write (1) please!\nIf it's too low then write (2) please!");
input = Console.ReadLine();
guess_number = Convert.ToInt32(input);
Console.WriteLine(" You select " + guess_number + " so, ");

do
{
counter += 1;

if (guess_number == 2)
{
min = middle + 1;
}
else if (guess_number == 1)
{
max = middle - 1;
}
else if (guess_number != 1 || guess_number != 2 || guess_number != 0)
{
Console.WriteLine(" Please write 0, 1 or 2 " + name);
}
middle = (min + max) / 2;
Console.WriteLine("Is your guess " + middle + " ?\nIf it's your guess then write (0) please!\nIf it's too high then write (1) please!\nIf it's too low then write (2) please!");

Console.WriteLine(counter + " times I tried for finding your number ");
} while (guess_number != 0);

Console.ReadKey();

但是,用户输入任何内容后总是重复输出,这是为什么,有没有办法获取数字?

最佳答案

根据你的描述,我认为你需要让用户在循环结束时向 guess_number 变量输入新值,否则循环不会从条件 guess_number != 0.

do
{
counter += 1;
if (guess_number == 2)
{
min = middle + 1;
}
else if (guess_number == 1)
{
max = middle - 1;
}
else if (guess_number != 1 || guess_number != 2 || guess_number != 0)
{
Console.WriteLine(" Please write 0, 1 or 2 " + name);
}
middle = (min + max) / 2;
Console.WriteLine("Is your guess " + middle + " ?\nIf it's your guess then write (0) please!\nIf it's too high then write (1) please!\nIf it's too low then write (2) please!");

Console.WriteLine(counter + " times I tried for finding your number ");

input = Console.ReadLine(); // let user key in new value.
guess_number = Convert.ToInt32(input);

} while (guess_number != 0);

关于c# - c# 中的数字猜谜游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52582843/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com