如何使用do-while循环程序解决此随机数猜谜游戏?

时间:2019-05-15 22:07:23

标签: java

创建一个程序,该程序会随机生成一个1-100的数字,并要求用户猜测它。如果用户输入的数字是低或高,则会显示一条消息告诉他们。当用户猜测随机数时,请告诉用户尝试了多少次才能获得该数字。之后,询问用户是否要再次执行此操作(如果用户确实重复生成新随机数的过程)。

问题是我似乎无法弄清楚如何让用户再次执行该操作,当我运行该程序时,似乎在代码中显示了错误。如果有人可以帮助我解决这个问题,那就太好了。谢谢!

import java.util.Scanner;
import java.util.Random;
public class RandomGuess
{
    public static void main(String [] args)
    {
        Scanner keyboard = new Scanner(System.in);
        Random randy = new Random();

        //#declaring variables
        int num, count = 0;
        final int random = randy.nextInt(100);
        String input;
        char yn;

        //#random number
        System.out.println("Num = " + random);

        //#title or header
        System.out.println("Random Number Guessing Game");
        System.out.println("===========================");

        //#asking user for input
        do
        {
            System.out.print("Guess the random number " + 
                "from 1 to 100===> ");
            num = keyboard.nextInt();

            //#if the number the user entered
            //#was less than the random number
            if(num < random)
            {
                //#display this message
                System.out.println("Your guess is too low try again...");
                System.out.println();
            }
            //#if the number the user entered
            //#was less than the random number
            if(num > random)
            {
                //#display this message
                System.out.println("Your guess is too high try again...");
                System.out.println();
            }
            count++;
            if (num == random)
            {
                System.out.println("You guessed the random number in " + 
                    count + " guesses!");
                break;
            }
            do 
            {
                System.out.print("Continue? (Y or N)==> ");
                input = keyboard.nextLine();
                yn = input.charAt(0);
            }
            while(yn == 'Y' || yn == 'y');
        }
        while (num > 1 || num > 100);

    }
}

1 个答案:

答案 0 :(得分:0)

您的代码有两个问题,甚至看不到显示的错误(我已经在这些区域添加了注释):

            count++;
            if (num == random)
            {
                System.out.println("You guessed the random number in " + 
                    count + " guesses!");
                break;
            }   // You should put an else here
            do 
            {
                System.out.print("Continue? (Y or N)==> ");
                input = keyboard.nextLine();
                yn = input.charAt(0);
            }
            while(yn == 'Y' || yn == 'y');  // This will keep asking if you want to try again so long as you enter a "y"
                     // But it won't actually let you try.
                     // Why? Because if you enter a y" it will loop back to the question.   
        }
        while (num > 1 || num > 100); // This should probably be (random != num)

    }
}

这是修订版

            count++;
            if (num == random) {
                System.out.println("You guessed the random number in " + 
                    count + " guesses!");
            } else {
                yn = 'x';    // can be anything other than y or n
                while(yn != 'y' && yn != 'n') {
                  System.out.print("Continue? (Y or N)==> ");
                  input = keyboard.nextLine();
                  yn = input.toLowerCase().charAt(0);
                }
            }
        }
        while (num != random && yn == 'y');
    }
}

希望这足以使您前进。

此外,请发布错误消息和/或错误原因说明,以及有关您实际要执行的操作的说明。

至于异常,问题在于Scanner.nextInt在您输入的数字末尾不占用换行符。因此,您的“继续Y / N”问题将获取上一行剩余的内容(即新行=>空字符串)。

您可以尝试以下方法:

    num = -1;       // Initialise the number to enable the loop
    while (num <= 1 || num >= 100) {
      System.out.print("Guess the random number from 1 to 100===> ");
      String ans = keyboard.nextline();
      try {
        num = Integer.parseInt();     // Convert the string to an integer - if possible
      } catch (NumberFormatException e) {
           // If the user's input can not be converted to an integer, we will end up here and display an error message.
        System.out.println ("Please enter an integer");
      }
    }