【问题标题】:Looping within switch statements在 switch 语句中循环
【发布时间】:2018-09-23 20:32:28
【问题描述】:

我想知道在 switch 语句中循环最有效的方法是什么。下面我有一个 userInput 变量,如果在这里可以更好地实现 if/then 语句以继续我的菜单选择,直到输入 -1 以退出我的程序,或者如果 do/while 循环更合适,我想提出建议。

import java.util.Scanner;

public class VirtualZoo
{
public static void main(String[] args) 
{
// Options
final int catType = 0,
          dogType = 1,
          duckType = 2,
          exit = -1;

// create Scanner
Scanner input;
input = new Scanner(System.in);
    int userInput;
                System.out.println("Welcome to the Zoo");
                System.out.println("Pick select an animal to visit");
    System.out.println("=================================");
    System.out.println("===========MAIN MENU=============");
    System.out.println("=================================");
    System.out.println("==  " + catType + ") Cat    ===================");
    System.out.println("==  " + dogType + ") Dog    ===================");
    System.out.println("==  " + duckType + ") Duck   ===================");
    System.out.println("== " + exit + ") EXIT   ===================");
    System.out.println("=================================");
    System.out.println();
    System.out.println();
    System.out.println( "Input  : ");
    Scanner sc = new Scanner(System.in);
    userInput = sc.nextInt();

Animal animalSelected = null;

switch (userInput) 
{
    case 0:
        animalSelected = new Cat();
        break;
    case 1:
        animalSelected = new Dog();
        break;
    case 2:
        animalSelected = new Duck();
        break;
    case -1:
        System.out.println("\n" + "Thank you for visiting the Virtual Zoo" + "\n" + "Goodbye!");
        break;
    default:
        break;
}

if (animalSelected != null)
 {
    System.out.println(animalSelected);
 }
}
}

【问题讨论】:

  • if then switch 不会循环你的执行。这将只是单程。那么问题出在哪里?
  • My issue is that I tried implementing a do while loop so that if the userInput, when -1 was selected, would exit my program, but if any other number is selected 0-2 the program would prompt你选择另一种动物。我基本上想要一个无限循环,直到用户决定退出,但是 do while 不适用于我的最终 if 语句。
  • 但是你给我看的代码没有循环
  • 那么为什么 domt ylu 首先解开你根本不工作的循环以及为什么它不工作以及如何解决这个问题。现在你的代码“工作正常”

标签: java loops if-statement do-while


【解决方案1】:

do while 循环比较合适,因为您总是希望至少运行一次 switch case。

【讨论】:

    【解决方案2】:

    在这种情况下添加一个 do-while 循环会更好,因为您希望它多次运行。 if/else 语句不会那么容易解决您的问题

    【讨论】:

      【解决方案3】:

      在这种情况下,do-while 循环是最好的。我将在此处使用 do-while 循环编写您的代码,如果您想要的话,请告诉我:

      import java.util.Scanner;
      
      public class VirtualZoo {
      
          public static void main(String[] args) {
      // Options
              final int catType = 0,
                      dogType = 1,
                      duckType = 2,
                      exit = -1;
      
      // create Scanner
              Scanner input;
              input = new Scanner(System.in);
              int userInput;
              System.out.println("Welcome to the Zoo");
              System.out.println("Pick select an animal to visit");
              System.out.println("=================================");
              System.out.println("===========MAIN MENU=============");
              System.out.println("=================================");
              System.out.println("==  " + catType + ") Cat    ===================");
              System.out.println("==  " + dogType + ") Dog    ===================");
              System.out.println("==  " + duckType + ") Duck   ===================");
              System.out.println("== " + exit + ") EXIT   ===================");
              System.out.println("=================================");
              do {
                  System.out.println();
                  System.out.println();
                  System.out.println("Input  : ");
                  Scanner sc = new Scanner(System.in);
                  userInput = sc.nextInt();
      
                  Animal animalSelected = null;
      
                  switch (userInput) {
                      case 0:
                          animalSelected = new Cat();
                          break;
                      case 1:
                          animalSelected = new Dog();
                          break;
                      case 2:
                          animalSelected = new Duck();
                          break;
                      case -1:
                          System.out.println("\n" + "Thank you for visiting the Virtual Zoo" + "\n" + "Goodbye!");
                          break;
                      default:
                          break;
                  }
      
                  if (animalSelected != null) {
                      System.out.println(animalSelected);
                  }
              } while (userInput != -1);
          }
      }
      

      【讨论】:

      • 是的,这就是我要尝试的方法,但每次我都会编写 while(userInput != -1);我会得到一个运行时错误。我一定是把它放错地方了。谢谢。
      • 不客气,我确实运行了相同的代码并且没有出现异常,如果您需要更多帮助,请在此处复制/粘贴您在日志中看到的异常。如果可以,请将答案标记为正确。玩得开心兄弟
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-05
      • 2013-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-09
      相关资源
      最近更新 更多