2014-02-08 116 views
-2

我需要幫助讓這個循環工作。這可能是因爲我真的不知道如何使用循環或if語句。但我覺得我需要經過10Java循環:爲什麼此循環沒有按預期停止?

import java.util.Scanner; 

public class Foothill { 
    public static void main(String args[]){ 
     Scanner scanner = new Scanner(System.in); 
     int numPurchases, totalPurchased; 
     boolean correct; 
     String menu; 
     numPurchases = 0; 
     totalPurchased = 0; 
     correct = false; 
     while(!correct) 
     { 
      System.out.println("Menu:"); 
      System.out.println(" P (Process Purchase)"); 
      System.out.println(" S (Shut down)"); 
      menu = scanner.nextLine(); 
      if (menu.charAt(0)=='S' || menu.charAt(0)=='s'){ 
       System.exit(0); 
      } else if (menu.charAt(0)=='P' || menu.charAt(0)=='p'){ 
       continue; 
      } 
      System.out.println("Your choice: " + menu.charAt(0)); 
      System.out.println("How many yougurts would you like to buy?"); 
      numPurchases = scanner.nextInt(); 
      totalPurchased += numPurchases; 
      if (totalPurchased > 0 || totalPurchased < 10){ 
       System.out.println("you have purchased " + totalPurchased + " yogurts"); 
      } else if (totalPurchased >= 10){ 
       System.out.println("Congradulations you have recieve a free Yogurt"); 
       System.out.println("Remainding yogurt credits: " + (totalPurchased-10)); 
      } 
     } 
    } 
} 

,當涉及到接收免費的酸奶使用IF語句我真的不知道如何使用循環,即舒服,所以,可能是我的問題,因爲我從來沒有提出利用虛假和真實

+8

標題後的建議有人溺水.. –

+0

你檢查過你的'while(!correct)'嗎? –

+2

'if(totalPurchased> 0 || totalPurchased <10){' - 每個數字大於0或小於10.您想'&&'。 – user2357112

回答

0

只需刪除下面的語句一個循環,那麼循環將工作:

else if (menu.charAt(0)=='P' || menu.charAt(0)=='p'){ 
       continue; 
      } 
0

你有許多問題與您的代碼。 這裏是解決方案:

import java.util.Scanner; 

public class Foothill { 
    public static void main(String args[]){ 
      Scanner scanner = new Scanner(System.in); 
      int numPurchases, totalPurchased; 
      boolean correct; 
      String menu; 
      numPurchases = 0; 
      totalPurchased = 0; 
      correct = false; 
      while(!correct){ 
       System.out.println("Menu:"); 
       System.out.println(" P (Process Purchase)"); 
       System.out.println(" S (Shut down)"); 
       menu = scanner.next(); 
       if (menu.charAt(0)=='S' || menu.charAt(0)=='s'){ 
       System.exit(0); 
       } 
       else if (menu.charAt(0)=='P' || menu.charAt(0)=='p'){ 
        System.out.println("Your choice: " + menu.charAt(0)); 
        System.out.println("How many yougurts would you like to buy?"); 
        numPurchases = scanner.nextInt(); 
        totalPurchased += numPurchases; 
       } 

       if (totalPurchased > 0 && totalPurchased < 10){ 
       System.out.println("you have purchased " + totalPurchased + " yogurts"); 
       } 
       else if (totalPurchased >= 10){ 
       System.out.println("Congradulations you have recieve a free Yogurt"); 
       System.out.println("Remainding yogurt credits: " + (totalPurchased-10)); 

       } 
      } 
      scanner.close(); 
     } 
    } 

就像上面提到的,你可能需要一些方法通過設置正確的true跳出while循環。

+0

哦,還要確保關閉掃描儀以防止資源泄漏。另一方面,你的代碼不檢查無效的輸入... –

0

由於user2357112已在評論中聲明,||運算符不適合測試數字是否在給定範圍內,因爲該條件對於任何數字都是如此。

您可以更改條件來代替使用&&,但我不知道爲什麼只在特定條件下顯示該消息。您可以在每次購買結束時打印它,然後在用戶收到免費贈品時打印額外的信息。

System.out.println("you have purchased " + totalPurchased + " yogurts"); 

if (totalPurchased >= 10){ 
    System.out.println("Congradulations you have recieve a free Yogurt"); 
    System.out.println("Remainding yogurt credits: " + (totalPurchased-10)); 
} 

你還需要知道Scanner#nextInt()does NOT consume the trailing new-line character來自用戶的輸入。但是,從讀取輸入中修剪此字符。因此,購買後你會得到一個StringIndexOutOfBoundsException,因爲Scanner#nextLine()在菜單中選擇用戶的選擇會吃掉尾隨的換行符,並且程序將嘗試獲取空字符串的第一個字符。

正如聯線表示,你可以通過閱讀整數輸入後,再次呼籲Scanner#nextLine()從緩衝區中刪除新行字符,像這樣解決這個問題:

numPurchases = scanner.nextInt(); 
scanner.nextLine(); 
+0

非常感謝你真的幫助。 – mwaymouth