程序的输出问题

时间:2018-10-07 02:56:01

标签: java

我想问一下为什么程序已经打印出答案了?即使我选择了不同的难度,它仍然是相同的。

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

public class GuessigGame {
    public static int level(int y) {
        Random ans = new Random();
        int easy = 20, medium = 50, hard = 10, x;
        Scanner in = new Scanner(System.in);
        System.out.println("Choose Difficulty");
        System.out.println("1. easy ");
        System.out.println("2. medium ");
        System.out.println("3. hard ");
        x = in.nextInt();

        switch (x) {
            case 1:
                System.out.println("Easy");
                y = easy;
                break;
            case 2:
                System.out.println("Medium");
                y = medium;
                break;
            case 3:
                System.out.println("Hard");
                y = hard;
                break;
            default:
                break;
        }
        return y;
    }

    public static void main(String[] args) {
        int choice, difficulty = 0, answer = -1;
        Scanner in = new Scanner(System.in);
        Random rand = new Random();

        System.out.println("\n\n1. Play Game");
        System.out.println("2. Exit");
        System.out.println("");
        choice = in.nextInt();
        int diff = 0, tries = 0, triesbot = 0, trieshu = 0;
        diff = level(difficulty);
        difficulty = 1 + rand.nextInt(diff);
        boolean win = false;
        switch (choice) {
            case 1:
                while (win != true) {
                    System.out.println(difficulty);
                    System.out.println("Tries: " + tries);
                    answer = in.nextInt();
                    if (answer == difficulty + 1 || answer == difficulty - 1) {
                        System.out.println("so close");
                    } else if (answer == difficulty + 2 || answer == difficulty + 2) {
                        System.out.println("youre answer was close");
                    } else if (answer == difficulty + 3 || answer == difficulty - 3) {
                        System.out.println("try more youre close");
                    } else if (answer == difficulty + 4 || answer == difficulty - 4) {
                        System.out.println("try youre best buddy!");
                    } else if (answer > difficulty + 4 || answer < difficulty - 4) {
                        System.out.println("so far!");
                    } else if (tries == 0) {
                        System.out.print("Game Over!");
                        win = true;
                    } else if (answer == difficulty) {
                        System.out.println("You got the correct answer!!!!");
                        win = true;
                    } else {
                    }
                    tries++;
                }
                break;
            default:
                System.exit(0);
                break;
        }
    }
}

这是程序的输出:

The output

1 个答案:

答案 0 :(得分:0)

因为您告诉计算机打印出困难。我建议使用更好的名字。

difficulty = 1 + rand.nextInt(diff);
boolean win = false;
switch(choice) {
    case 1:
        while(win != true) {
            System.out.println(difficulty);
            System.out.println("Tries: " + tries);