特定选择后,Switch语句停止编译

时间:2018-10-28 23:57:36

标签: java switch-statement

我已经编写了一个ATM程序,但是在mainMenuOptions()方法中似乎出现了问题。我的switch语句似乎都可以正常工作,并且可以循环回来输入用户ID,以及选择要使用ATM进行的操作。当用户进入第4种情况时,所有内容都会按照预期的方式打印,但菜单永不返回,因此您可以继续进行操作。如果在情况4的输出之后输入任何数字,您只会收到消息BUILD SUCCESSFUL,并且必须重新启动程序。

我需要案例4与案例1、2、3和5的功能相同。我需要选择它并在下面产生输出并继续执行程序。

预期产量

主菜单

  1. 检查余额

  2. 提现

  3. 存款

  4. 帐户信息

  5. 退出(选择其他帐户)

请选择一个:4

帐户创建时间:美国东部夏令时间2018年10月23日星期二19:27:22

帐户利率为:0.65

帐户余额为:44.0

主菜单

  1. 检查余额

  2. 提现

  3. 存款

  4. 帐户信息

  5. 退出(选择其他帐户)

实际输出

主菜单

  1. 检查余额

  2. 提现

  3. 存款

  4. 帐户信息

  5. 退出(选择其他帐户)

请选择一个:4

帐户创建时间:美国东部夏令时间2018年10月23日星期二19:27:22

帐户利率为:0.65

帐户余额为:44.0

成功构建

我的代码在下面.....

import java.util.Date;
import java.util.Scanner;



public class test {

    private static Account[] accounts = new Account[10];

    public static void main(String[] args) {



        accounts();
        mainMenuOptions();
    }
    //main menu option method
    public static void mainMenuOptions() {
        Scanner input = new Scanner(System.in);

        Date d = new Date();

        int enterchoice = -1;
        int id=-1;
        while (enterchoice != 4) {
            mainMenu();
            //enter id
            System.out.println("Enter an id: ");
            id = input.nextInt();
            System.out.println("Enter choice: ");
            enterchoice = input.nextInt();

            int index = -1;
            for (int i = 0; i < accounts.length; i++) {
                if (accounts[i].getid() == id) {
                    index = i;
                    break;
                }
            }
            switch (enterchoice) {
            case 1:
                System.out.println("The balance is " + accounts[index].getbalance());
                break;
            case 2:
                {
                    System.out.println("Enter an amount to withdraw ");
                    double amount = input.nextDouble();
                    //withdraw method
                    accounts[index].withdraw(amount);
                    break;
                }
            case 3:
                {
                    System.out.println("Enter an amount to deposit ");
                    double amount = input.nextDouble();
                    //deposit method
                    accounts[index].deposit(amount);
                    break;
                }
            case 4:
                {
                    System.out.println("Account was created on: " + d.toString());
                    System.out.println("Account interest rate is: " + accounts[index].getMonthlyInterestRate());
                    System.out.println("Account balance is: $" + accounts[index].getbalance());
                    double amount = input.nextDouble();
                    //display date account created, account interest rate, and balance
                    accounts[index].deposit(amount);
                    break;
                }
            case 5:
                {
                    System.out.println("Exit (choose a different account)");
                    double amount = input.nextDouble();
                    //loop back through program to make new account selection
                    break;
                }
            default:
                break;
            }
        }
    }

    public static void accounts() {
        //create accounts
        //initialize 100 dollars
        for (int i = 0; i < accounts.length; i++) {
            accounts[i] = new Account(i, 100);
        }
    }

    //main menu method
    public static void mainMenu() {

        System.out.println("Main menu" + "\nEnter a choice" + "\n1:Check Balance" + "\n2:Withdraw" + "\n3:Deposit" + "\n4:Account Information" + "\n5:Exit (choose a different account)");

    }

    //main menu method
    public static void mainMenus() {
        System.out.println("Main menu" + "\nEnter a choice" + "\n1:Check Balance" + "\n2:Withdraw" + "\n3:Deposit" + "\n4:Account Information" + "\n5:Exit (choose a different account)");

    }

    private String dateCreated;
}
class Account{
    private int id = 0;
    private double balance = 0;
    private static double annualInterestRate = 7.8;
    private final double withdraw;
    private final double deposit;
    private final double amount;
    private java.util.Date dateCreated;
    private Object getMonthlyInterestRate;

    Account(){
        this.amount = 0;
        this.withdraw = 0;
        this.deposit = 0;
        this.dateCreated.toString();
        this.getMonthlyInterestRate.toString(); 

    }


    Account(int id, double balance){
        this.amount = 0;
        this.withdraw = 0;
        this.deposit = 0;
        this.id = id;
        this.balance = balance;
    }

    public int getid(){
        return this.id;

    }
    public void setid(int newid){
        id = newid;
    }
    public double getbalance(){
        return this.balance;
    }
    public double getMonthlyInterestRate() {
        return (annualInterestRate / 12);
    }

    public double getMonthlyInterest() {
        return balance * getMonthlyInterestRate();
    }

    public void withdraw(double amount){
        balance = balance - amount;
    }

    public void deposit(double amount){
        balance = balance + amount;
    }


}

1 个答案:

答案 0 :(得分:0)

我发现,如果将while循环从while (enterchoice != 4)更改为while (enterchoice < 6),我的代码将按需要运行。