在switch语句中循环

时间:2018-09-23 20:32:29

标签: java loops if-statement do-while

我想知道在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);
 }
}
}

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

在这种情况下,添加do-while循环会更好,因为您希望它多次运行。 if / else语句无法轻松解决您的问题

答案 2 :(得分:-1)

在这种情况下,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);
    }
}