有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何修复这个随机数猜测游戏使用dowhile循环程序?

创建一个程序,随机生成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) 个答案

  1. # 1 楼答案

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

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

    希望这足以推动你前进

    此外,请发布错误消息和/或描述它做错了什么,以及描述您实际希望它做什么

    至于例外情况,问题在于扫描仪。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");
          }
        }