不能在java中重复切换语句

时间:2016-10-24 17:30:42

标签: java switch-statement console-application

我正在为我的计算机Science类在java中开发一个简单的银行应用程序,但我无法重复切换语句。这是我的switch语句。谢谢!

//Menu
System.out.println("W. Withdraw");
System.out.println("D. Deposit");
System.out.println("B. View Balance");
System.out.println("To exit program, type Quit");

Scanner keyboardMenu = new Scanner(System.in);
menuOptions = keyboardMenu.next();

//Switch statement to navigate menu options
switch (menuOptions) {
    case "B": 
        System.out.println("Your balance is $" + balance);
        break;
    case "D":
        System.out.println("Deposit: ");
        Scanner keyboardDeposit = new Scanner(System.in);
        double deposit = keyboardDeposit.nextDouble();
        balance = balance + deposit;
        System.out.println("Your new balance is $" + balance);
        break;
    case "W":
        System.out.println("Withdraw: ");
        Scanner keyboardWithdraw = new Scanner(System.in);
        double withdraw = keyboardWithdraw.nextDouble();
        balance = balance - withdraw;
        System.out.println("Your new balance is $" + balance);
        break;
    case "Quit":
        System.out.println("Program terminated.");
        break;
    default:
        System.out.println("Please select an option.");
        break;
    } 
}

2 个答案:

答案 0 :(得分:0)

如果没有更多的背景,我会假设你正在尝试做这样的事情:

do{
    System.out.println("W. Withdraw");
    System.out.println("D. Deposit");
    System.out.println("B. View Balance");
    System.out.println("To exit program, type Quit");
    Scanner keyboardMenu = new Scanner(System.in);
    menuOptions = keyboardMenu.next();
    boolean keepGoing = true; 
   //Switch statement to navigate menu options
   switch (menuOptions) {
case "B": 
       System.out.println("Your balance is $" + balance);
       break;
case "D":
       System.out.println("Deposit: ");
       Scanner keyboardDeposit = new Scanner(System.in);
    double deposit = keyboardDeposit.nextDouble();
    balance = balance + deposit;
    System.out.println("Your new balance is $" + balance);
    break;
case "W":
    System.out.println("Withdraw: ");
    Scanner keyboardWithdraw = new Scanner(System.in);
    double withdraw = keyboardWithdraw.nextDouble();
    balance = balance - withdraw;
    System.out.println("Your new balance is $" + balance);
    break;
case "Quit":
    System.out.println("Program terminated.");
    keepGoing = false; 
    break;
default:
    System.out.println("Please select an option.");
    break;
}while(keepGoing)

答案 1 :(得分:0)

我会将Quit子句拉出开关,然后将其他函数包装在While循环中。类似的东西:

//Switch statement to navigate menu options
while (menuOptions != "Quit") {
    switch (menuOptions) {
        case "B": 
            System.out.println("Your balance is $" + balance);
            break;
        case "D":
            System.out.println("Deposit: ");
            double deposit = keyboardDeposit.nextDouble();
            balance = balance + deposit;
            System.out.println("Your new balance is $" + balance);
            break;
        case "W":
            System.out.println("Withdraw: ");
            double withdraw = keyboardWithdraw.nextDouble();
            balance = balance - withdraw;
            System.out.println("Your new balance is $" + balance);
            break;
        default:
            System.out.println("Please select an option.");
            break;
    } 
    menuOptions = keyboardMenu.next();
}
System.out.println("Program terminated.");

因此,每次进入切换时,您都确定他们没有输入Quit。然后,在一天结束时,当他们输入Quit时,你会进入Program Terminated行。

我还会将new Scanner放在Switch声明之后。这样你只需要调用一次......在你做完决策树之后。