Java HiLo程序停止

时间:2016-06-21 10:45:22

标签: java

现在我在java中制作一个HiLo游戏并且我遇到了一些问题,不确定是否我正在做正确的事情,因为我是非常新的java。

我在获得两个整数时遇到问题"回答"并且"猜测"到第3种方法。 我必须在这项任务中使用3种方法。

方法1:询问用户想要的是什么。

方法2:程序在max之间选择一个随机数,让用户猜测。

方法3:将查看猜测是否= =要回答,如果没有,它会让用户再次尝试。同时我必须保存用户的所有尝试。

import java.util.Scanner;

import java.util.Random;

public class oneagain {

public static void main(String[] args) {

    System.out.println("Welcome HiLo!");
    System.out.println("What lvl?");
    System.out.println("1. Easy (1-10)");
    System.out.println("2. Medium (1-100)");
    System.out.println("3. Hard (1-1000)");

    Scanner sc = new Scanner(System.in);

    int choice = sc.nextInt();

    int tries = (playGame(choice));

}

public static int playGame(int choice) {
    Scanner sc = new Scanner(System.in);
    int tries = 0;
    if (choice == 1) {
        System.out.println("Guess a number between 1 and 10");

        int answer = (int) (Math.random() * 11) + 1;
        int guess = sc.nextInt();
    }
    if (choice == 2) {
        System.out.println("Guess a number between 1 and 100");

        int answer = (int) (Math.random() * 101) + 1;
        int guess = sc.nextInt();
    }
    if (choice == 3) {
        System.out.println("Guess a number between 1 and 1000");

        int answer = (int) (Math.random() * 1001) + 1;
        int guess = sc.nextInt();
    }
    return tries;
}

public static void giveResponse(int answer, int guess) {

    if (choice == 1) {
        int tries = 0;
        Scanner sc = new Scanner(System.in);
        boolean y = false;

        while (y == false) {

            guess = sc.nextInt();
            tries++;

            if (guess == answer) {
                y = true;

            } else if (guess < answer) {
                System.out.println("Guess was too low!");
                System.out.println("Guess a number between 1 and 10:");
            } else if (guess > answer) {
                System.out.println("Guess was too high!");
                System.out.println("Guess a number between 1 and 10:");
            } else if (guess == answer) {
                System.out.println("Score!");
                System.out.println("You succeeded in " + tries + " attempt");
            }

            if (choice == 2) {

                while (y == false) {

                    guess = sc.nextInt();
                    tries++;

                    if (guess == answer) {
                        y = true;

                    } else if (guess < answer) {
                        System.out.println("Guess was too low!");
                        System.out.println("Guess a number between 1 and 100:");
                    } else if (guess > answer) {
                        System.out.println("Guess was too high!");
                        System.out.println("Guess a number between 1 and 100:");
                    } else if (guess == answer) {
                        System.out.println("Score!");
                        System.out.println("You succeeded in " + tries + " attempt");
                    }

                    if (choice == 3) {

                        while (y == false) {

                            guess = sc.nextInt();
                            tries++;

                            if (guess == answer) {
                                y = true;

                            } else if (guess < answer) {
                                System.out.println("Guess was too low!");
                                System.out.println("Guess a number between 1 and 100:");
                            } else if (guess > answer) {
                                System.out.println("Guess was too high!");
                                System.out.println("Guess a number between 1 and 100:");
                            } else if (guess == answer) {
                                System.out.println("Score!");
                                System.out.println("You succeeded in " + tries + " attempt");
                            }
                        }
                    }
                }
            }
        }
    }
}

}

0 个答案:

没有答案