java - 超过数组长度后仍存储用户输入

标签 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.");
      }

   }

}

最佳答案

您并没有真正使用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++;
    }
}

(对于其他饼图类型也是如此)。

关于java - 超过数组长度后仍存储用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31358254/

相关文章:

java - 如何调用以方法作为参数的方法?

java - 错误: “Method getText() must be called from the UI thread, currently inferred thread is worker.”

C# : Difference between method types

java - Firebase Glide 不显示图像

java - 从 SOAP 安全 header 获取 X 509 证书

java - 将类更改为泛型类型

c++ - 我想知道为什么用 vector 可以成功而用数组却不行?

java - 修复覆盖错误

java - 对第二个单词进行排序

javascript - Angular - 我可以将两个数组分开并在 ng-repeat 中使用它们吗?