【问题标题】:Need help fixing difficulty selection for guessing game in java [closed]需要帮助修复Java中猜谜游戏的难度选择[关闭]
【发布时间】:2013-11-27 22:54:51
【问题描述】:

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

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;

            }
        }
    }

【问题讨论】:

  • 您的问题应该始终包括您的程序应该做什么以及它当前做什么。如果他们不得不猜测所有这些,这里的用户将不太愿意帮助你。
  • 如果是崩溃,堆栈跟踪会有所帮助
  • “有人可以修复我的代码并重新发布它,因为我现在有点迷路了!” SO 不是“完成我的作业”服务。问一个更具体的问题,因为这个问题可能会被关闭。
  • 这个问题似乎是题外话,因为它是关于完成某人的家庭作业。

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


【解决方案1】:

不要将switchif 用于同一目的(并将它们放在一起)。选一个。两者都很好。

在你这里建造:

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

}

if 永远不会是真的,因为你把它放在case 1 块中。显然dif不能同时是1和3。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-09
    • 1970-01-01
    • 2019-08-14
    • 1970-01-01
    • 2014-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多