JAVA号码猜测游戏使用方法:再次玩法不能正常工作

时间:2015-02-02 08:31:04

标签: java search methods binary

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

程序运行时应该通过猜测下限和上限的中点,用户响应更高或更低,直到猜到正确的数字。但是,当程序询问您是否想再玩一次? (是/否):它没有正确输入字符。无论输入什么字符,程序都会再次运行,并且不会输入" 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;
    }
}

4 个答案:

答案 0 :(得分:3)

问题在于:

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

你问玩家是否想要再进行一轮然后......不做任何事情并且无论如何都要进行另一轮比赛。试试这个:

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

答案 1 :(得分:2)

您在不使用返回值的情况下调用shouldPlayAgain()

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

答案 2 :(得分:0)

当您编写此代码时,您在代码中没有退出条件进行迭代:

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

            }

你可以写:

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

然后将控件返回到主循环。

此致

答案 3 :(得分:0)

尝试运行以下代码。我试图调整你的代码,包括条件改进和循环删除。

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

输出: -

  

运行:

     

猜一个1到100之间的数字。

     

号码是50吗? (h / l / c):l

     

RESONSE:l

     

号码是25吗? (h / l / c):c

     

RESONSE:c

     

大!你想再玩一次吗? (y / n):y

     

TEMP:true

     

号码是25吗? (h / l / c):c

     

RESONSE:c

     

大!你想再玩一次吗? (y / n):n

     

突破循环

对于更改,请阅读添加的注释。希望这会有所帮助。