Java HiLo游戏 - 无法生成新的猜测

时间:2017-07-08 15:16:02

标签: java

抱歉我知道这个游戏还有其他帖子,但我的问题是具体的。我刚刚开始学习Java并将这个HiLo游戏整合在一起,但我很难将两件事情搞砸 -

1)如果用户想再次玩,我如何让它生成一个新的选择?现在,如果你再次玩,你会获得相同的选择 2)如果用户选择中止游戏,我没有收到生成消息

请帮助我,我是新手,容易受到Java的攻击!

package lab4;

import java.util.Scanner;
import java.util.Random;

public class hilo 
{

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        //randomly pick a number between 1-100
        Random rand = new Random();
        int pick = rand.nextInt(100) + 1;
        int count = 0;
        boolean play = true;

        //prompt user to guess      
        Scanner scan = new Scanner(System.in);
        System.out.println("*---------------------------------*");
        System.out.println("* Guess a number between 1-100:   *");
        System.out.println("* (enter \"0\" to end at any time)  *");
        System.out.println("*---------------------------------*");
        int guess = scan.nextInt();

        //on each guess, say if too low or too high
        while (play = true)
        {   
            while (guess != pick && guess !=0)
            {

                if (guess < pick)
                {
                    count++;
                    System.out.println("Higher...");
                    System.out.println(pick);
                    guess = scan.nextInt();
                }

                else if (guess > pick)
                {  
                    count++;
                    System.out.println("Lower...");
                    System.out.println(pick);
                    guess = scan.nextInt();
                }

            }


            if (guess == pick)
            {   
                System.out.println("You got it! It only took you " + count + " guesses.");
                System.out.println("Want to play again?");
                System.out.println("* Guess a number between 1-100:   *");
                System.out.println("* (enter \"0\" to end at any time)  *");
                guess = scan.nextInt();
                if (guess != 0)
                    play = true;
                else
                    play = false;
            }
        }

        if (play = false || guess == 0) 
        {
            System.out.println("Bummer. See you next time!");
        }

     }
}

2 个答案:

答案 0 :(得分:2)

两件小事,

int pick = rand.nextInt(100) + 1;
// ...

//on each guess, say if too low or too high
while (play = true)
{   

一个=是赋值,两个==是相等的。所以while (play == true)(详细) while (play)。然后将pick移到循环中。

// int pick = rand.nextInt(100) + 1;
// ...
while (play) {   
    int pick = rand.nextInt(100) + 1;

这应该可以解决这两个问题。

答案 1 :(得分:0)

只需在if条件中为pick变量指定新值,即可测试guesspick

的相等性
if (guess == pick)
{   
  System.out.println("You got it! It only took you " + count + " guesses.");
  System.out.println("Want to play again?");
  System.out.println("* Guess a number between 1-100:   *");
  System.out.println("* (enter \"0\" to end at any time)  *");
  guess = scan.nextInt();

  pick = rand.nextInt(100) + 1; // new value

  if (guess != 0)
    play = true;
  else
    play = false;
}