2013-05-15 22 views
0

我該如何更改我的代碼以使它在運行一次後重新運行if/else-if語句。如何讓我的方法在執行一次後重新啓動?

E.g.我怎麼能去: 頂部菜單>提示符下輸入(即2)>子菜單>提示符下輸入(是1或2)>添加/刪除>返回頂部菜單/子菜單

/** 
* A simple program of a grocery store, which assists 
* the purchases, calculate total price and display bill. 
**/ 

public class GroceryStore { 

    // this method manages the entire shopping process 
    public void start() { 
     final String UPI = "wcor690"; // a constant for student UPI 

     Stock stock = new Stock(); 
     stock.loadItems("stock.txt"); 
     Cart cart = new Cart(); 

     System.out.println("=============================================================="); 
     System.out.println("------This is a simple grocery store program by " + UPI + ".------"); 
     topMenu(); 
     int input = getChoice(1, 3); 
     if (input == 1){ 
      stock.displayItems(); 
      topMenu(); 
     } 
     else if (input == 2){ 
      subMenu(); 
      input = getChoice(1, 4); 
      if (input == 1){ 
       String itemCode = Keyboard.readInput(); 
       cart.addItem(stock.findItem(itemCode)); 
       topMenu(); 
      } 
      else if (input == 2){ 
       String itemCode = Keyboard.readInput(); 
       cart.deleteItem(itemCode); 
       subMenu(); 
      } 
      else if (input == 3){ 
       cart.checkOut(); 
      } 
      else if (input == 4){ 
       cart.deleteAll(); 
       System.out.println("All items are cleared from the shopping cart"); 
       topMenu(); 
      } 
     } 
     else if (input == 3){ 
      System.out.println("Exit"); 
     } 

     stock.saveItems("stock2.txt"); 
     System.out.println("--------------------------------------------------------------"); 
     System.out.println("---------------Thank you for shopping with us!----------------"); 
     System.out.println("=============================================================="); 
    } 

    // this method displays the top-level menu 
    private void topMenu() { 
     System.out.println("1. Show items"); 
     System.out.println("2. Start shopping online"); 
     System.out.println("3. Exit"); 
    } 

    // this method displays the second-level menu 
    private void subMenu() { 
     System.out.println("1. Add item"); 
     System.out.println("2. Remove item"); 
     System.out.println("3. Checkout"); 
     System.out.println("4. Exit without buying"); 
    } 

    // this method gets the user's input choice 
    private int getChoice(int lower, int upper) { 
     String userInput = Keyboard.readInput(); 
     int input = Integer.parseInt(userInput); 
     while (input<lower || input>upper){ 
      System.out.println("Invalid choice, please enter a valid choice"); 
      input = Integer.parseInt(Keyboard.readInput()); 
     } 
     return input; 
    } 

} 

回答

2

您可以使用某種的循環,這將繼續超出代碼,直到滿足條件。

結賬this link

下面是一個例子:

do { 
    code line 1; 
    code line 2; 
    code line 3; 
    ... 
} while(yourCondition); 

如果yourCondition滿足(yourCondition == true),該代碼將回到code line 1(將執行dowhile之間的代碼塊),它會停止,一旦條件不滿意(yourCondition == false)。

現在,在瞭解它的工作原理之後,您可以輕鬆將其應用於您的代碼。

相關問題