JAVA 猜数游戏使用方法:重玩方法不正常

JAVA number guessing game using methods: method for play again does not work properly

我是 JAVA 的新手,我编写了一个程序来计算用户在 1 到 100 之间选择的数字。

程序 运行 应该通过猜测下限和上限的中点以及用户响应的更高或更低,直到猜出正确的数字。但是,当程序询问您是否想再玩一次? (y/n): 它没有正确输入字符。无论输入什么字符,程序都会再次 运行,并且不会在输入 "n" 时终止。

任何帮助将不胜感激。谢谢!

import java.util.Scanner;

public class NumberGuesser 
{

    public static void main(String[] args)
    {
        do
        {
            playOneGame();
        }
        while(shouldPlayAgain());
    }

    //method for complete guessing number game between 1 and 100
    public static void playOneGame()
    {
        //initial lower range to 0 and upper range to 100
        int lowerBound = 0;
        int upperBound = 100;

        //Display message for user to guess a nummber
        System.out.println("Guess a number between 1 and 100.");

        //declare variables for program's number guess and user's response if number is higher or lower
        int guess;
        char userResponse;

        do //do while loop to make number guesses until correct number is found
        {
            //call method to guess midpoint number
            guess = Midpoint(lowerBound,upperBound);

            // call method to get response from user if number is high low or correct
            userResponse = getUserResponseToGuess(guess);

            //decision structure if-else statements to set new lower/upper bounds after every iteration 
            if (userResponse == 'h' || userResponse == 'H')
            {
                lowerBound = guess;
            }else if (userResponse == 'l' || userResponse == 'L')
            {
                upperBound = guess;
            }else if (userResponse == 'c' || userResponse == 'C')
            {
                shouldPlayAgain();
                playOneGame();
            }

        }while(userResponse != 'c' || userResponse != 'C');

    }

    //method to ask and allow user if they would like to play the game again
    public static boolean shouldPlayAgain()
    {
        //Prompt User if they would like to play again
        System.out.print("Great! Would you like to play again? (y/n): ");

        //Read in a character
        Scanner input = new Scanner(System.in);
        char value = input.next().charAt(0);

        //if true return y char
        return value == 'y' || value == 'Y';
    }

    //method to get response from user for computer to guess if number is high, low, or correct
    public static char getUserResponseToGuess(int guess)
    {
        //char response = guess;
        Scanner input = new Scanner(System.in);

        //declare local variable for user's response
        char response;

        do
        {
            //print out message with guess number and take input of h/l/c
            System.out.print("Is the number " + guess + "? (h/l/c): ");

            //Read in character
            response = input.next().charAt(0);

        }while(response != 'h' && response != 'l' && response != 'c'); //display guess and get user input while input does not equal h/l/c

        return response;
    }

    //method to find the midpoint from two integers. take smaller number if 2 to return with 2 integers
    public static int Midpoint(int low, int high)
    {
        int midpoint; //midpoint is a local variable

        midpoint = (high+low)/2; //compute midpoint between upper and lower bounds

        return midpoint;
    }
}

您调用 shouldPlayAgain() 时未使用 return 值。

         if (shouldPlayAgain()){
            playOneGame();
          }

问题在这里:

        }else if (userResponse == 'c' || userResponse == 'C')
        {
            shouldPlayAgain();
            playOneGame();
        }

你问玩家是否想再玩一轮,然后...什么也不做,运行 无论如何再玩一轮。试试这个:

        {
            if(shouldPlayAgain()){
                playOneGame();
            }
        }

当您编写以下代码时,您正在迭代且代码中没有退出条件:

else if (userResponse == 'c' || userResponse == 'C')
            {
                shouldPlayAgain();
                playOneGame();

            }

你可以这样写:

else if (userResponse == 'c' || userResponse == 'C') { break; }

然后你return控制主循环。

此致,

尝试 运行 下面的代码。我已尝试调整您的代码,其中包括条件改进和循环删除。

package test;

import java.util.Scanner;

public class NumberGuesser 
{

    public static void main(String[] args)
    {
/*Reduntant loop removal by codechef*/        
//        do
//        {
            playOneGame();
//        }
//        while(/*shouldPlayAgain()*/false);
    }

    //method for complete guessing number game between 1 and 100
    public static void playOneGame()
    {
        //initial lower range to 0 and upper range to 100
        int lowerBound = 0;
        int upperBound = 100;

        //Display message for user to guess a nummber
        System.out.println("Guess a number between 1 and 100.");

        //declare variables for program's number guess and user's response if number is higher or lower
        int guess;
        char userResponse;
         boolean temp = true;
         one :do //do while loop to make number guesses until correct number is found
        {
            //call method to guess midpoint number
            guess = Midpoint(lowerBound,upperBound);

            // call method to get response from user if number is high low or correct
            userResponse = getUserResponseToGuess(guess);

            //decision structure if-else statements to set new lower/upper bounds after every iteration 
            if (userResponse == 'h' || userResponse == 'H')
            {
                lowerBound = guess;
            }else if (userResponse == 'l' || userResponse == 'L')
            {
                upperBound = guess;
            }else if (userResponse == 'c' || userResponse == 'C')
            {
                /* Block restructured by codechef */
                temp = shouldPlayAgain();
                if(temp)
                {
                    System.out.println("TEMP : "+temp);
                    //playOneGame();
                    continue one;
                }else{
                    System.out.println("Breaking Loop");
                    break one;
                }
            }
            /*condition restructured by codechef*/
        }while((userResponse != 'c' || userResponse != 'C' )&& temp);

    }

    //method to ask and allow user if they would like to play the game again
    public static boolean shouldPlayAgain()
    {
        //Prompt User if they would like to play again
        System.out.print("Great! Would you like to play again? (y/n): ");

        //Read in a character
        Scanner input = new Scanner(System.in);
        char value = input.next().charAt(0);

        //if true return y char
        /* statement restructured by codechef */
        return (value=='y'||value=='Y')?true:false;
    }

    //method to get response from user for computer to guess if number is high, low, or correct
    public static char getUserResponseToGuess(int guess)
    {
        //char response = guess;
        Scanner input = new Scanner(System.in);

        //declare local variable for user's response
        char response;
/* Reduntant While loop removal by codechef*/
//        do
//        {
            //print out message with guess number and take input of h/l/c
            System.out.print("Is the number " + guess + "? (h/l/c): ");

            //Read in character
            response = input.next().charAt(0);
            System.out.println("RESONSE : "+response);
//        }while(response != 'h' && response != 'l' && response != 'c'); //display guess and get user input while input does not equal h/l/c

        return response;
    }

    //method to find the midpoint from two integers. take smaller number if 2 to return with 2 integers
    public static int Midpoint(int low, int high)
    {
        int midpoint; //midpoint is a local variable

        midpoint = (high+low)/2; //compute midpoint between upper and lower bounds

        return midpoint;
    }
}

输出:-

run:

Guess a number between 1 and 100.

Is the number 50? (h/l/c): l

RESONSE : l

Is the number 25? (h/l/c): c

RESONSE : c

Great! Would you like to play again? (y/n): y

TEMP : true

Is the number 25? (h/l/c): c

RESONSE : c

Great! Would you like to play again? (y/n): n

Breaking Loop

有关更改,请阅读添加的评论。希望这有帮助。