使用循环/仅一个命令返回到命令选项的麻烦正在运行(JAVA)

时间:2014-07-03 22:17:29

标签: java loops

这是我第一次访问这个网站。我现在正在学习Java课程,我在使用这个代码/程序时遇到了一些麻烦,我想让用户选择是否想看到"好猴子",&#34 ;坏猴子"或者"显示猴子"。它远没有完成,但在命令完成后我无法返回命令屏幕/区域。我希望尽可能多地使用这些命令。其次,如果有人放入" Good Monkey",我的程序会处理所有输入。因此,如果你输入一个类似"菠萝"的字样,它仍然会用为#34; Good Monkeys"指定的输出来迎接你。输入

我已经在网上看到了,也许我应该使用" do-while"循环并使用"切换"。任何输入/帮助将不胜感激。非常感谢!

这是我的代码:公共类和公共静态和扫描程序导入都在这段代码中,但出于某种原因我不能在不破坏代码格式的情况下将它们添加到这篇文章中。

 Scanner jScanner = new Scanner(System.in);
 System.out.println("please enter Good Monkeys, Bad Monkeys or Show Monkeys");
 String userChoice = jScanner.nextLine();   
 for (int b= 1; b < 11000; b++) 
    {
        if (userChoice.equalsIgnoreCase("Good Monkeys"));
        {
            System.out.println("You have selected Good Monkeys");
            System.out.println("How many monkeys do you want? Put in a integer between 3 and 20");
            Scanner goodMonkeyScanner = new Scanner (System.in);
            int userChoiceGood = goodMonkeyScanner.nextInt();
            if (userChoiceGood >= 3 && userChoiceGood <= 20)
            {
                System.out.println("Here you go");
                System.out.println("Monkeys (metapohorical)");
            break;
        }
        else if (userChoice.equalsIgnoreCase("Bad Monkeys"))
        {
            System.out.println("You have selected Bad Monkeys");
            System.out.println("How many monkeys do you want? Put in a integer between 3 and 20");
                Scanner badMonkeyScanner = new Scanner (System.in);
                int userChoiceBad = badMonkeyScanner.nextInt();
                if (userChoiceBad >= 3 && userChoiceBad <= 20)
                {
                    System.out.println("Here you go");
                    System.out.println("Monkeys (metapohorical)");
                    break; 
                }
                else 
                    System.out.println("Sorry this doesn't work");  

        }   

        else if ((userChoice.equalsIgnoreCase("Show Monkeys")))
        {   
            System.out.println("Monkeys");
            System.out.println("0");
            System.out.println("\\/");
            System.out.println(" |");
            System.out.println("/\\");
            break;
        }
        else 
        {   
            System.out.println(" Wrong Answer. Try again");
        }

        break;
    }

}
}

}

1 个答案:

答案 0 :(得分:1)

首先,您需要定义循环。其次,您需要将输入指令放在循环中。

我会包含一个done变量来检测用户何时想逃离

所以,让我们的代码:

Scanner jScanner = new Scanner(System.in);
boolean done = false;
while(!done) {
    System.out.println("please enter Good Monkeys, Bad Monkeys or Show Monkeys");
    System.out.println("(or enter 'done' to exit");
    String userChoice = jScanner.nextLine();
    swithc(userChoice.toLowerCase()) {
        case "good monkeys":
            /*
             * The code for this option
             */
             break;
        case "bad monkeys":
            /*
             * The code for this option
             */
             break;
        case "show monkeys":
            /*
             * The code for this option
             */
             break;
        case "done":
            done = true;
            break;
        default:
            System.out.println("Your input isn't what I expected!\nTry again!");
            break;
    }
}

代码,解释:

  • while(!done)内容可以被理解为&#34;而未完成&#39;做以下事情&#34;
  • userChoice.toLowerCase():我将userChoice转换为小写,以简化比较。这样,我只需要将字符串与其他小写字符串进行比较
  • switch(userChoice.toLowerCase()):......嗯......我想你可以自己搞清楚;)
    • 如果没有其他案例有效则会发生default阻止
    • "done"块会将done变量设置为true,因此会终止循环
    • 重要提示: 始终 case
    • 结束break

进一步阅读:

另外,我建议您学习Flowcharts,然后在开始编码之前,尝试用纸绘制程序的流程图。这样,在开始编写第一行代码之前,您将获得程序的清晰图像。