需要帮助修复java中猜谜游戏的难度选择

时间:2013-11-27 22:54:52

标签: java if-statement while-loop switch-statement

我需要帮助让我的猜谜游戏让你选择选择哪个难度,有人可以修改我的代码并重新发布它因为我现在有点丢失了!

import java.util.*;

public class GuessingGame
{
public static void main(String[] args)
{
    boolean play = true;
    String input1;

    while(play == true){
        Scanner input = new Scanner(System.in);
        Random rand = new Random();
        int counter = 0;
        int guess = -1;
        int dif = 0;
        int easy = rand.nextInt(10) + 1, med = rand.nextInt(1000) + 1, hard = rand.nextInt(10000) + 1; 

        System.out.println("Difficulty Select");
        System.out.println("=================");
        System.out.print("1) Easy   2) Medium   3) Hard     :");
        dif = input.nextInt();

        switch (dif)
        {
            case 1:
                if (dif == 1)
                    System.out.println ("Random number between 1 and 10 selected." + easy);
                else if (dif == 3)
                    System.out.println ("Random number between 1 and 10000 selected.");
            break;


        }
        while (guess != med)
        {
            System.out.print ("|" + med + "|" + "Random number between 1 and 1000 selected.");
            guess = input.nextInt();
            counter = counter + 1;

            if (guess == med)
                System.out.println ("YOU WIN MOFO!");
            else if (guess < med)
                System.out.println ("You're to cold!");
            else if (guess > med)
                System.out.println ("You're to hot!");
        }

        System.out.println ("It took you " + counter + " guess(es) to get it correct"); 
        System.out.print ("Do you want to play again? (y/n): ");
        input1 = input.nextLine();  // absorb enter key from integer
        input1 = input.nextLine();

        if (input1.equals("y"))
           play = true;
        else
           play = false;

            }
        }
    }

1 个答案:

答案 0 :(得分:2)

不要将switchif组合用于相同目的(并将它们放在彼此内)。选一个。这两个都很好。

在你的建筑中:

switch (dif)
{
    case 1:
        if (dif == 3) ...

}

if if永远不会成立,因为你将它放在case 1块中。显然,dif不能同时为1和3。