其他声明不起作用

时间:2017-09-27 00:29:46

标签: c#

int number = 4;
int guesscount = 0;
int guess;

Console.WriteLine("Guess a number between 1 and 10: ");
guess = Convert.ToInt32(Console.ReadLine());

while (guess != number)
{
    guesscount = guesscount + 1;

    if (guess < number)
    {
        Console.WriteLine("Your guess is too low");
    }
    else if (guess > number)
    {
        Console.WriteLine("Your guess is too high");
    }
    else
    {
        Console.WriteLine("You got it!!");
    }
    Console.WriteLine("Guess again: ");
    guess = Convert.ToInt32(Console.ReadLine());
    Console.ReadLine();
}

当运行代码时if和else if语句有效,但是当猜测正确的数字(4)时,消息“你得到它”不会显示窗口只是关闭

5 个答案:

答案 0 :(得分:1)

如果您在循环中,number不等于AND (SECONDARY_ISSUE IS NULL OR SECONDARY_ISSUE != 'VALUE_A') 在运营商处查看您的状况。

答案 1 :(得分:1)

如果键入正确的数字,则while循环表达式为false(猜测等于数字),以便不执行代码块。这是一个有效的解决方案:

    public static void Main(string[] args) {
        int number = 4;
        int guesscount = 1;
        int guess;
        string guessMessage;

        Console.WriteLine("Guess a number between 1 and 10: ");
        guess = GetNumber();

        while (guess != number) {

            if (guess < number) {
                Console.WriteLine("Your guess is too low");
            }
            else if (guess > number) {
                Console.WriteLine("Your guess is too high");
            }

            Console.WriteLine("Guess again: ");
            guess = GetNumber();
            guesscount++;
        }

        if (guesscount == 1)
            guessMessage = "Well done!!! You got it first time!";
        else 
            guessMessage = "You got it!! It took " + guesscount + " guesses.";

        Console.WriteLine(guessMessage);
        Console.ReadLine();
    }

    private static int GetNumber() {
        int number;

        while (!Int32.TryParse(Console.ReadLine(), out number)) {
            Console.WriteLine("That was not a number!\nGuess again: ");
        }

        return number;
    }

答案 2 :(得分:0)

可能是因为在第一次猜测之后,如果键入了正确的数字,程序会在尝试再次显示消息之前退出循环吗?

为什么不尝试do while循环:

int number = 4;
int guesscount = 0;
int guess;

do {
    if (guesscount == 0) {
        Console.WriteLine("Guess a number between 1 and 10: ");
    } else {
        Console.WriteLine("Guess again: ");
    }
    guesscount = guesscount + 1;
    guess = Convert.ToInt32(Console.ReadLine());
    Console.ReadLine();

    if (guess < number)
    {
        Console.WriteLine("Your guess is too low");
    }
    else if (guess > number)
    {
        Console.WriteLine("Your guess is too high");
    }
    else
    {
        Console.WriteLine("You got it!!");
    }
} while (guess != number)

答案 3 :(得分:0)

首先,你可以选择&#34;你得到它&#34;循环外的指令,它将在猜测等于数字时运行(这就是你要找的东西)。循环将在您不猜数字的情况下工作,因此只有大于和小于此处的评估。这是一个有效的代码:

    int number = 4;
    int guesscount = 0;
    int guess = 0;

    Console.WriteLine("Guess a number between 1 and 10: ");    
    guess = Convert.ToInt32(Console.ReadLine());

    while(number != guess){
        if(guess < number)
            Console.WriteLine("Your guess is too low");
        else
            Console.WriteLine("Your guess is too high");

        Console.WriteLine("Guess again: ");
        guesscount = guesscount + 1;
        guess = Convert.ToInt32(Console.ReadLine());
    }

    Console.WriteLine("You got it!!!!");

答案 4 :(得分:-3)

Errrm ..... 请问“再猜一次”吗? 它工作,然后添加控制台。

之后的Readkey()
 Console.WriteLine("You got it!!");