用户输入在超出数组长度后仍然存储

时间:2015-07-11 14:36:01

标签: java arrays methods

有人能看出为什么用户可以输入超过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.");
      }

   }

}

2 个答案:

答案 0 :(得分: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++;
    }
}

(和其他派对类型一样)。

答案 1 :(得分: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声明为循环之外的私有静态,并引用它。