Java - 如何退出do while循环?

时间:2016-02-13 15:17:57

标签: java

public class GetUserResponse
{
    private List<UserRecord> userRecordList;

    public List<UserRecord> UserRecordList
    {
        get { return userRecordList; }
        set { userRecordList = value; }
    }
}

public class UserRecord
{
    private string userId;
    private List<TimePeriod> timePeriodList;

    public string UserId
    {
        get { return userId; }
        set { userId = value; }
    }

    public List<RbeActivationPeriod> TimePeriodList
    {
        get { return timePeriodList; }
        set { timePeriodList = value; }
    }
}

public class TimePeriod
{
    private DateTime startTime;
    private DateTime endTime;

    public DateTime StartTime
    {
        get { return startTime; }
        set { startTime = value; }
    }

    public DateTime EndTime
    {
        get { return endTime; }
        set { endTime = value; }
    }
}

如果用户输入数字public static void main(String[] args) { // TODO Auto-generated method stub KeyboardReader reader = new KeyboardReader (); double money = 0; //Cash Variable int Choice = 0; //Menu Choice Scanner AccountIn = new Scanner(System.in); //Scanner for AccountNumbers ArrayList<String> AccountNumber = new ArrayList<String>(); String accountValue = ""; int x = 1; do{ try{ System.out.println("Create Account Number"); accountValue = AccountIn.nextLine(); if(accountValue.length()>7 && accountValue.length()<9){ // EveryTime AccountNumber Is created store in Array AccountNumber.add(accountValue); //ADD USER INPUT money = reader.readDouble("Enter Starting Amount Of Money"); do { System.out.println("1.Deposit"); System.out.println("2.Withdraw"); System.out.println("3.View Account Details"); System.out.println("4.Exit"); Choice = reader.readInt(""); //Selects Choice switch (Choice) { case 1: //Money is incremented by the amount listed money+=reader.readDouble(); System.out.println("Current Amount" + money); break; case 2: money -= reader.readDouble(); System.out.println("Curent Amount" + money); break; case 3: System.out.println(AccountNumber); System.out.println("Final Amount:" + money); } } while(Choice != 4); //Exit Do While Loop System.out.println("Final Amount:" + money); System.out.println("Goodbye"); }else { throw new Exception(); } } catch (Exception e) { System.out.println("Enter Valid Number"); } } while (x==1); } 并且只显示最终金额和消息'再见',我正在尝试退出程序,现在它会循环回到顶部显示消息“创建帐号“。

1 个答案:

答案 0 :(得分:1)

在案例3之后再添加1个案例:(添加休息;对案例3也是如此)

case 4:
   x = 0; // while(x==1) needs to stop, so you need to set it something other than 1, maybe 0.
}