无法在猜猜游戏类中找到错误

时间:2012-04-23 17:08:55

标签: java class

好的,这是猜谜游戏完整代码的一部分。

public static void Game(){              //Second page of game with option of continue playing when guessed close enough to answer.
    Scanner scan = new Scanner(System.in);

    GuessingGame testgame=new GuessingGame();

    testgame.Generator();
    int num = testgame.GetGenNum();
    //Produces a random number between 1 and 100 inclusive of 1 and 100


    System.out.println("Guess the target integer which is between 1 and 100 inclusive");

    int guess=scan.nextInt();

    int difference=(guess-num);



    while(guess!=num){
        int yesCounter=0;

        System.out.println("Guess again " +num+" "+difference);
        guess=scan.nextInt();
        difference=(guess-num);


        ///something wrong here, shouldnt repeat if difference too big 
        if(difference<=5||difference>=-5){  //something wrong with the condition, it says its close enough even tho it isnt.    
        while(yesCounter<1){
            System.out.println("Close enough, do you want to keep Guessing? Y/N ");
            yesCounter++;


            String play = scan.nextLine();
            //play=play1;
            while(!(play.equalsIgnoreCase("y")))
            {


                if(play.equalsIgnoreCase("n")){
                    System.exit(1);
                }

                else{
                    if((play.equalsIgnoreCase("y"))){
                        invalid();
                        guess=scan.nextInt();
                    }
                    else{
                        Game();             ///TAKE note as it might restart the game with new random integer
                    }
                }

            }
        }

    }   

    }

输出是:

.........................

播放? Y / N

ý

猜测1到100之间的目标整数

50

再次猜猜44 6

44

足够近,你想继续猜猜吗?是/否

猜测1到100之间的目标整数

..........................

问题是,当用户猜出一个数字时,条件是如果猜测和生成的数字之间的差异是5或更小,告诉用户它足够接近,并询问用户是否想继续猜测,但条件是没有实现,但仍在运行,有人可以帮忙吗?

3 个答案:

答案 0 :(得分:0)

while(!(play.equalsIgnoreCase("y")))
        {


            if(play.equalsIgnoreCase("n")){
                System.exit(1);
            }

            else{
                if((play.equalsIgnoreCase("y"))){
                    invalid();
                    guess=scan.nextInt();
 ....

这是不对的,if((play.equalsIgnoreCase("y")))永远不会是真的,因为它所在的循环,如果是真的则无法明确地输入。这就是你的问题所在,它将始终重启游戏,因为它在else分支中调用了Game()。简而言之,这就是你所做的:

boolean _true = true;

while(! _true) {

    if(_true) {
        //impossible
    }
    else {
        Game(); //ALWAYS
    }
}

由于你用家庭作业标记了你的问题,我不会给出完整的修正,但现在你知道它出了什么问题,你应该能够弄清楚你需要改变什么才能推进:)

答案 1 :(得分:-1)

您混合了以及(注意:您使用的条件(difference<=5||difference>=-5)始终为true。)。您可以使用以下任何一种

if (difference<=5 && difference>=-5) { ... }    # difference is 5 or less

if (difference<=-5 || difference>=5) { ... }    # difference is 5 or more

如果您使用Math.abs(...),则更易读:

if (Math.abs(difference)<=5) { ... }    # difference is 5 or less

if (Math.abs(difference)>=5) { ... }    # difference is 5 or more

RESP。

答案 2 :(得分:-1)

if(difference<=5||difference>=-5)这表示如果差异小于5或大于-5。所有数字都小于5或大于-5,因此总是为真。

我假设你想要的是if(Math.abs(difference)<=5)。这将告诉您差异变量的绝对值是否小于或等于5.