2015-07-11 84 views
1

有人可以看到爲什麼用戶可以輸入超過27個蘋果,藍莓或花生餡餅嗎?即使在爲每個類型的餅圖的最大數量聲明最終的int之後。超過數組長度後仍然存儲用戶輸入

此處的對象是不斷提示用戶餡餅的類型,直到用戶想要退出。每次輸入一個有效輸入時,它都存儲在它自己的數組中。用戶表示完成後,計算完成並打印一條消息。

import javax.swing.JOptionPane; 

public class CalcPieProfit { 

    public static void main(String[] args) { 

     final int MAX_PER_TYPE = 27; 

     int appleTotal = 0; 
     int blueberryTotal = 0; 
     int peanutTotal = 0; 

     String typeOfPie = getPieType(); 
     while (!typeOfPie.equalsIgnoreCase("q")) { 
     if (typeOfPie.equalsIgnoreCase("apple")) { 
      String[] appleArray = fillApple(typeOfPie, MAX_PER_TYPE); 
      appleTotal++; 
     } 
     else if (typeOfPie.equalsIgnoreCase("blueberry")) { 
      String[] blueberryArray = fillBlueberry(typeOfPie, MAX_PER_TYPE); 
      blueberryTotal++; 
     } 
     else if (typeOfPie.equalsIgnoreCase("peanut")) { 
      String[] peanutArray = fillPeanut(typeOfPie, MAX_PER_TYPE); 
      peanutTotal++; 
     } 
     typeOfPie = getPieType(); 
     } 

     if (typeOfPie.equalsIgnoreCase("q")) { 
     int totalPies = calcTotalPies(appleTotal, blueberryTotal, peanutTotal); 
     double profit = calcProfit(appleTotal, blueberryTotal, peanutTotal); 
     printReport(totalPies, appleTotal, blueberryTotal, peanutTotal, profit); 

     } 

    } 

    public static String getPieType() { 

     String pieType; 

     do {  
     try { 

      pieType = JOptionPane.showInputDialog("Enter a pie type:");   
     }   
     catch (NumberFormatException e) {   
      pieType = "";   
     }  
     if (!pieType.equalsIgnoreCase("apple") && !pieType.equalsIgnoreCase("blueberry") && 
     !pieType.equalsIgnoreCase("peanut") && !pieType.equalsIgnoreCase("q")) {   
      JOptionPane.showMessageDialog(null, "Enter 'apple', 'blueberry', 'peanut', or 'q' only.");   
     }  
     } while (!pieType.equalsIgnoreCase("apple") && !pieType.equalsIgnoreCase("blueberry") && 
     !pieType.equalsIgnoreCase("peanut") && !pieType.equalsIgnoreCase("q")); 

     return pieType; 

    } 

    public static String[] fillApple(String typeOfPie, int MAX_PER_TYPE) { 

     String[] appleArray = new String[MAX_PER_TYPE]; 

     for (int i = 0; i < appleArray.length; i++) { 

     appleArray[i] = typeOfPie; 

     } 

     return appleArray; 

    } 

    public static String[] fillBlueberry(String typeOfPie, int MAX_PER_TYPE) { 

     String[] blueberryArray = new String[MAX_PER_TYPE]; 

     for (int i = 0; i < blueberryArray.length; i++) { 

     blueberryArray[i] = typeOfPie; 

     } 

     return blueberryArray; 

    } 

    public static String[] fillPeanut(String typeOfPie, int MAX_PER_TYPE) { 

     String[] peanutArray = new String[MAX_PER_TYPE]; 

     for (int i = 0; i < peanutArray.length; i++) { 

     peanutArray[i] = typeOfPie; 

     } 

     return peanutArray; 

    } 

    public static int calcTotalPies(int appleTotal, int blueberryTotal, int peanutTotal) { 

     int total = appleTotal + blueberryTotal + peanutTotal; 

     return total; 

    } 

    public static double calcProfit (int appleTotal, int blueberryTotal, int peanutTotal) { 

     final double APPLE_PROFIT = 5.94; 
     final double BLUEBERRY_PROFIT = 5.89; 
     final double PEANUT_PROFIT = 6.95; 

     double profit = (APPLE_PROFIT * appleTotal) + (BLUEBERRY_PROFIT * blueberryTotal) + 
     (PEANUT_PROFIT * peanutTotal); 

     return profit; 

    } 

    public static void printReport(int totalPies, int appleTotal, int blueberryTotal, int peanutTotal, double profit) { 

     if (totalPies > 0) { 
     JOptionPane.showMessageDialog(null, 
      "Pie Report\n\n" + 
      "Total pies: " + totalPies + 
      "\nTotal of apple pie: " + appleTotal + 
      "\nTotal of blueberry pie: " + blueberryTotal + 
      "\nTotal of peanut butter pie: " + peanutTotal + 
      "\nTotal profit: $" + String.format("%.2f", profit)); 
     } 
     else { 
     JOptionPane.showMessageDialog(null, "Enjoy your day off."); 
     } 

    } 

} 

回答

0

你是不是真的使用String[]小號appleArrayblueberryArraypeanutArray - 他們在各自的方法創建,但沒有其他任何地方使用。爲了計算利潤,你是(正確的)只有總變量。

而不是

if (typeOfPie.equalsIgnoreCase("apple")) { 
    String[] appleArray = fillApple(typeOfPie, MAX_PER_TYPE); 
    appleTotal++; 
} 

,你應該這樣做

if (typeOfPie.equalsIgnoreCase("apple")) { 
    if (appleTotal >= MAX_PER_TYPE) { 
     JOptionPane.showMessageDialog(null, "Too many apples."); 
    } else { 
     appleTotal++; 
    } 
} 

(與同爲其他餡餅類型)。

+0

嗯我怎麼能使用字符串[] s來跟蹤總數而不是使用++ – dan

+0

這是可能的,但你不應該,因爲數組有固定的長度。改用['List's](http://tutorials.jenkov.com/java-collections/list.html)。 – Glorfindel

0

你每次去添加它們時都會重新聲明餅圖數組。

public static String[] fillApple(String typeOfPie, int MAX_PER_TYPE) { 
    String[] appleArray = new String[MAX_PER_TYPE]; 
    for (int i = 0; i < appleArray.length; i++) { 
     appleArray[i] = typeOfPie; 
    } 
    return appleArray; 
} 

每次調用此方法時,都會生成一個新的「appleArray」。如果您希望它在對此方法的調用之間保持不變,請將appleArray聲明爲循環外部的私有靜態,並引用它。

+0

對不起,我還是很困惑..你能告訴我一個例子嗎? – dan

相關問題