有 Java 编程相关的问题?

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

java在while循环中遇到问题,并试图通过按字母来终止程序

我的程序有问题

该程序是一个HILO游戏,要求用户在生成的数字范围内猜测一个数字

问题是,一旦我在键盘上按0,它就会显示随机数,基本上必须结束循环并询问用户是想继续还是结束程序。相反,它继续循环,每当我按下键盘上的一个字母时,它就会显示一个Exception错误,该错误描述并输入未匹配。有人能告诉我怎么修吗?,也许我把程序写错了,我尝试了多种方法,但它仍然不能正常工作。 多谢各位

import java.util.Scanner;
import java.util.Random;
import java.lang.Math;

public class HILOGAME
{
    public static void firstGame()
    {
        final int range = 50;
        int answer = 0;
        int guesses;
        int number;
        int guessNum = 0;

        String choice = "";
        boolean Loop = false;

        Scanner input = new Scanner(System.in);
        Random r = new Random();

        answer = r.nextInt(range) + 1;

        System.out.println("IF YOU WISH TO GIVE UP, PRESS 0 ON THE KEYBOARD");

        while (!Loop)
        {
            for (int exit = 0; exit < 10; exit++)
            {
                System.out.print("Guess a number between 1 and " + range + " : ");
                number = input.nextInt();
                guessNum = guessNum++;
                guessNum += 1;

                if (number == answer)
                {
                    System.out.println(" Your guess was correct ");
                    System.out.println();
                    System.out.println("The number was: " + answer);
                    System.out.println("You guess the number with: " + guessNum + " guesses ");
                    System.out.println();
                    System.out.println("Enter x to continue to play or y to endgame");
                    choice = input.nextLine();

                    if (!choice.equalsIgnoreCase("x"))
                    {
                        Loop = true;
                        answer = r.nextInt(range) + 1;
                        System.out.print("A new number");
                        break;
                    }

                    else if (choice.equalsIgnoreCase("y"))
                    {
                        Loop = false;
                        System.out.print("END OF GAME");
                        exit = 11;
                    }
                }

                if (number > answer)
                {
                    System.out.println("TOO HIGH");
                }
                if (number < answer)
                {
                    System.out.println("TOO LOW");
                }

                if (number == 0)
                {
                    System.out.println("YOU GAVE UP. THE NUMBER WAS " + answer);
                    System.out.println("Enter x to continue to play or y to endgame");
                }

            }
        }
    }
}

共 (3) 个答案

  1. # 1 楼答案

    编辑1:

    这段代码非常适合我,我试过运行它:

    final int range = 50;
        int answer = 0;
        int guesses;
        int number;
        int guessNum = 0;
    
        String choice = "";
        boolean Loop = false;
    
        Scanner input = new Scanner(System.in);
        Random r = new Random();
    
        answer = r.nextInt(range) + 1;
    
        System.out.println("IF YOU WISH TO GIVE UP, PRESS 0 ON THE KEYBOARD");
    
        while (!Loop)
        {
            int exit;
            for (exit = 0; exit < 10; exit++)
            {
                System.out.print("Guess a number between 1 and " + range + " : ");
                number = input.nextInt();
                guessNum = guessNum++;
                guessNum += 1;
    
                if (number == answer)
                {
                    System.out.println(" Your guess was correct ");
                    System.out.println();
                    System.out.println("The number was: " + answer);
                    System.out.println("You guess the number with: " + guessNum + " guesses ");
                    System.out.println();
                    System.out.println("Enter x to continue to play or y to endgame");
                    choice = input.next();
    
                    // HERE YOU ARE DOING THE WRONG THING
                    // What is the meaning of "not(!)" here ? !choice.equalsIgnoreCase("x")
                    // Remove "NOT(!)" from here your program will work as expected
    
                    if (choice.equalsIgnoreCase("x"))
                    {
                        //here you should assign loop to false if they continue
                        Loop = false;
                        answer = r.nextInt(range) + 1;
                        System.out.print("A new number");
                        break;
                    }
                    else if (choice.equalsIgnoreCase("y"))
                    {
                        //better to insert break here as well
                        //here you should assign loop to true if they wanna exit.
                        Loop = true;
                        System.out.print("END OF GAME");
                        exit = 11;
                        break;
                    }
                }
    
                else if (number > answer)
                {
                    System.out.println("TOO HIGH");
                }
                else if (number < answer)
                {
                    System.out.println("TOO LOW");
                }
    
                if (number == 0)
                {
                    System.out.println("YOU GAVE UP. THE NUMBER WAS " + answer);
                    System.out.println("Enter x to continue to play or y to endgame");
    
                    // MADE CHANGES HERE
    
                    choice = input.next();
    
                    if (choice.equalsIgnoreCase("x"))
                    {
                        //here you should assign loop to false if they continue
                        Loop = false;
                        answer = r.nextInt(range) + 1;
                        System.out.print("A new number");
                        break;
                    }
                    else if (choice.equalsIgnoreCase("y"))
                    {
                        //better to insert break here as well
                        //here you should assign loop to true if they wanna exit.
                        Loop = true;
                        System.out.print("END OF GAME");
                        exit = 11;
                        break;
                    }
                }
    
            }
        }
    

    请复制粘贴代码。因为它毫无例外地工作得很好

    我的示例输出:

    示例1:

    Guess a number between 1 and 50 : 1
    TOO LOW
    Guess a number between 1 and 50 : 0
    TOO LOW
    YOU GAVE UP. THE NUMBER WAS 27
    Enter x to continue to play or y to endgame
    y
    END OF GAME
    

    样本2:

    Guess a number between 1 and 50 : 20
    TOO HIGH
    Guess a number between 1 and 50 : 19
    TOO HIGH
    Guess a number between 1 and 50 : 15
     Your guess was correct 
    
    The number was: 15
    You guess the number with: 3 guesses 
    
    Enter x to continue to play or y to endgame
    x
    A new numberGuess a number between 1 and 50 : 20
    TOO LOW
    Guess a number between 1 and 50 : 0
    TOO LOW
    YOU GAVE UP. THE NUMBER WAS 42
    Enter x to continue to play or y to endgame
    y
    END OF GAME
    

    如果有什么问题一定要告诉我

  2. # 2 楼答案

    小小的修改

    public class HILOGAME {
        public static void firstGame() {
            final int range = 50;
            int answer;
            int number;
            int guessNum = 0;
    
            String choice = "";
            Scanner input = new Scanner(System.in);
            Random r = new Random();
    
            answer = r.nextInt(range) + 1;
    
            System.out.print("Guess a number between 1 and " + range + " : ");
            number = input.nextInt();
            if (number == answer) {
                System.out.println(" Your guess was correct ");
                System.out.println();
                System.out.println("The number was: " + answer);
                System.out.println("You guess the number with: " + guessNum + " guesses ");
                System.out.println();
            } else {
                System.out.println("you entered a wrong value");
                System.out.println("do you really want to try again? press y to continue or other alphabet to exit");
                choice = input.next();
                playAgain(choice);
            }
    
        }
    
        public static void playAgain(String choice) {
            if (choice.equalsIgnoreCase("Y")) {
                firstGame();
            } else {
                System.out.println("you chose to quit");
                System.exit(0);
            }
        }
    
  3. # 3 楼答案

    如果你想结束一个循环,你可以使用break;命令。它跳出whilefor循环。如果数字是0,你可以用这个:

    if (number == 0)
    {
      System.out.println("YOU GAVE UP. THE NUMBER WAS " + answer);
      System.out.println("Enter 'x' to continue to play or y to endgame");
      choice = input.nextLine();
      if(choice == "y")
      {
        break;
      }
    

    并加上: if(choice == "y") {break;}while(!Loop)循环的最末端

    当答案正确时,将Loop设置为false,这意味着!Loop将计算为true。当他们决定继续游戏时,你应该将Loop = false改为Loop = true