Looping within switch statements

Rocks

New Member
#1
I am wondering what the most efficient method for looping within a switch statement would be. Below I have a userInput variable and would like suggestions if an if/then statement would be better implemented here to continue my menu selection until -1 is entered to exit my program, or if a do/while loop would be more appropriate.
Mã:
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);
 }
}
}
 

Admin

Administrator
Thành viên BQT
#2
In this cases a do-while loop would be the best one. I will write your code with a do-while loop here, please let me know if it is what you want:
Mã:
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);
    }
}
 
OP
OP
R

Rocks

New Member
#3
yes that is what I was going to try but every time I would code the while(userInput != -1); I would get a runtime error. I must have misplaced it in the structure. Thank you.
 

Từ khóa phổ biến

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top