猜猜游戏又麻烦了

时间:2014-09-24 04:01:20

标签: java if-statement while-loop

好的,所以我尝试了一种方法,但它一直在显示"你想再玩一次吗?#34;每次我因某种原因输入数字。这是我到目前为止所拥有的。我还能做些什么,以便只有在用户猜到正确答案后才询问他/她是否想再次玩?

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

public class GuessNumber {

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

        Random rand = new Random();
        int number = rand.nextInt(100) + 1;
        int guess;

        System.out.println("Guess the number between 1 and 100\n");

        guess = scan.nextInt();

        while (true) {
            if(guess < number)       
               System.out.println("Higher!");
            else if(guess > number)
               System.out.println("Lower!");
            else if (guess == number){
               System.out.println("Correct!");
            }
               guess = scan.nextInt();
       }
    }
  }

3 个答案:

答案 0 :(得分:1)

试试这个

class GuessNumber {

    static Random rand = new Random();
    static Scanner scan = new Scanner(System.in);
    static int number;

    public static void main(String[] args) {
        playGame();
    }

    public static void playGame() {
        number = rand.nextInt(100) + 1;
        System.out.println("Guess the number between 1 and 100");
        while (true) {
            int guess = scan.nextInt();
            if (guess < number) {
                System.out.println("Higher!");
            } else if (guess > number) {
                System.out.println("Lower!");
            } else if (guess == number) {
                System.out.println("Correct!");
                System.out.println("Do you like to play again?[1 for Yes/0 for No]");
                int val = scan2.next();
                if (val == 1)
                    playGame();
                else
                    break;
            }
        }
    }
}

或者不是使用相同的扫描仪,您可以使用另一台扫描仪并获取字符串输入,如下所示

 else if (guess == number) {
      System.out.println("Correct!");
      Scanner scan2 = new Scanner(System.in);
      System.out.println("Do you like to play again?[Y/N]");
      String val = scan2.next();
      if (val.equalsIgnoreCase("Y"))
           playGame();
      else 
           break;
 }

答案 1 :(得分:0)

正确时你需要退出。

这样做,在行之后:

System.out.println("Correct!");

添加另一行,如下所示:

break;

答案 2 :(得分:0)

主要是出于乐趣,我实施的游戏符合您尝试做的描述。代码不是最干净/最有效的,但绝对有效!

看看它,看看你是否能解决问题。

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

public class GuessingGame {

public static void main(String[] args) {

        System.out.println("Guess the number between 1 and 100");

        playGuessingGame();

        while (true) {
            System.out
                .println("Do you want to play again? Type y / n and hit enter");

            Scanner scan = new Scanner(System.in);
            String playAgain = scan.next();

            if (playAgain.charAt(0) == 'y') {
                System.out.println("Okay, I picked a new number. Good luck!");
                playGuessingGame();
            } else {
                System.out.println("Thanks for playing!");
                break;
            }
        }

    }

    public static void playGuessingGame() {
        Scanner scan = new Scanner(System.in);
        Random rand = new Random();

        int guess;
        int number = rand.nextInt(100) + 1;

        guess = scan.nextInt();

        while (guess != number) {
            if (guess < number)
                System.out.println("Higher!");
            else if (guess > number)
                System.out.println("Lower!");
            else if (guess == number) {
                System.out.println("Correct!");
            }
            guess = scan.nextInt();
        }
    }
}