【问题标题】:Why is my do-while loop breaking too early in java?为什么我的 do-while 循环在 java 中过早中断?
【发布时间】:2018-10-10 13:43:21
【问题描述】:

所以我的问题是,当它运行时,一旦你输入一个,它只会打印下一个开关并结束程序,而不允许输入下一个选项。这是我到目前为止所做的。

  1. 我已将 while(choice == 1) 放入案例 1 中。
  2. 我已经采取了 while(choice == 1) 并且只是使用了开关 在 switch 和使用 continue 语句中。
  3. 当我取出最后两次休息时,我得到了 无限循环。

任何关于我正在做什么的提示都会有所帮助。

package Spook;

import java.util.Scanner;

public class House {

    Scanner in = new Scanner(System.in);

    static void p(String I) {
        System.out.println(I);
    }

    public void game() {

        p("\nWelcome To Spook House, were all Spooks will haunt you.");

        do {
            p("\nPlease make your selection");
            p("         1.  Enter House for some Scares.");
            p("         2. Too Scared to Enter.");
            p("         3. Really Scared Please let me exit.");
            p("Choose one please.");

            char choice = in.next().charAt(0);

            switch (choice) {
                case '1':
                    p("\nAs prepare to enter the house, The door slowly creaks open.");
                    p("\nYou enter the house and the door slams shut.");
                    p("What do you do????");
                    p("         1. Try to open the door.");
                    p("         2. Find the nearist closet and hide.");
                    p("         3. Continue onward.");
                    p("         4. Faint and end game.");
                    break;
                case '2':
                    p("\nWhat are you a chicken, Just press 1!!!!!");
                    break;
                case '3':
                    p("\nFine you win chicken, now ending.");
                    System.exit(0);
                    break;
            }
            while (choice == '1') {
                switch (choice) {
                    case '1':
                        p("\nAs you twist the door knoob and try to pull it open. You feel a gust of wind that pushes you down.");
                        break;
                    case '2':
                        p("\nYou run towards the closet hoping to hide till daylight.");
                        p("You start to shake and laugh nerviously.");
                        break;
                    case '3':
                        p("\nYou explore the first room.");
                        p("You see an old crooked picture of a scary clown.");
                        break;
                    case '4':
                        p("\nYou have been easily to SPOOKED MMMMUUUUHHHHAAAAHHAAAA!!!!!");
                        System.exit(0);
                        break;
                }
                break;
            }
            break;
        } while (true);
    }
}

【问题讨论】:

  • 请修正您的代码格式。
  • 只是一件小事,“都是”应该是“在哪里”。 “准备进入”应为“准备进入”。 “最近的”应该是“最近的”。 “紧张”应该是“紧张”。 :-)
  • 你真的试过调试你的代码吗?连续 3 行中的 3 次中断闻起来很麻烦。
  • 提示:查看你的中断语句。将它们与你的循环和开关关联起来,看看哪个 break 语句中断了。
  • 感谢那些评论。

标签: java switch-statement do-while


【解决方案1】:

如果条件应该可以正常工作,请尝试在内部使用 break

例子:

if(choice == '4')
  break;

【讨论】:

    【解决方案2】:
                        System.exit(0);
                        break;
    

    可以写成

                        return;
    

    【讨论】:

      【解决方案3】:

      您缺少要求用户输入下一个输入的代码行。 试试这个:

      switch (choice) {
                  case '1':
                      p("\nAs prepare to enter the house, The door slowly creaks open.");
                      p("\nYou enter the house and the door slams shut.");
                      p("What do you do????");
                      p("         1. Try to open the door.");
                      p("         2. Find the nearist closet and hide.");
                      p("         3. Continue onward.");
                      p("         4. Faint and end game.");
                      choice = in.next().charAt(0);
                      break;
      

      在这行中,用户被再次询问输入,然后应用程序终止。

      【讨论】:

      • 有趣的是我有选择权。但是由于某种原因,当我去复制它时,当我试图让代码框显示时,我必须删除它。不过还是谢谢
      【解决方案4】:

      那么您的代码的作用是打印带有“请进行选择”和 3 个选项的第一个文本块。然后你抓住用户输入(所以'1')。

      它进入第一个 switch 语句并进入case '1'。它打印出它需要的内容并跳出那个 switch 语句。

      然后进入while (choice == '1') 循环。在这里,您立即输入第二个switch case,而无需等待并获取新的输入。 因为您不等待并获取新输入,所以choice 的值仍然是'1'

      因此,在第二个switch case 中,您立即输入case '1'。 在这种情况下,它会打印需要打印的内容。然后你跳出switch case,跳出while (choice == '1')循环,最后跳出do while循环。

      【讨论】:

      • 谢谢你,这真的很有帮助,结果证明是问题的 90%。
      猜你喜欢
      • 1970-01-01
      • 2021-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多