2016-02-13 157 views
-2
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); 
} 

我想如果用户输入的数字4并与最终的量和消息“再见”才显示退出程序,现在它循环右后卫顶端显示消息“创建帐户号码”。Java - 如何退出do while循环?

+0

询问一些问题这样的尝试做一些研究,开始于甲骨文例如文档之前。 –

回答

1

添加1更多的案例情况后3:(加休息;过于案例3)

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