2015-07-11 45 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

相关问题