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

Java 超过数组长度后仍存储用户输入,java,arrays,methods,Java,Arrays,Methods,有人知道为什么用户可以输入超过27个苹果、蓝莓或花生派吗?即使在为每种饼图类型的最大数量声明了最终int之后 这里的目的是不断提示用户输入饼图类型,直到用户想要退出。每次输入一个有效输入时,它都存储在自己的数组中。用户指示完成后,计算完成并打印消息 import javax.swing.JOptionPane; public class CalcPieProfit { public static void main(String[] args) { final int M

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

   }

}
import javax.swing.JOptionPane;
公共类CalcPieProfit{
公共静态void main(字符串[]args){
每种类型的最终整数最大值=27;
int appleTotal=0;
int蓝莓总量=0;
int peanutTotal=0;
字符串typeOfPie=getPieType();
而(!typeof pie.equalsIgnoreCase(“q”)){
if(PIE.equalsIgnoreCase(“苹果”)的类型){
字符串[]applarray=fillApple(类型为Pie,每种类型的最大值);
appleTotal++;
}
else if(类型为Pie.equalsIgnoreCase(“蓝莓”)){
String[]blueberryArray=fillBlueberry(类型为Pie,每种类型的最大值);
蓝莓醇++;
}
else if(类等信号(“花生”)){
字符串[]peanutArray=FillPeant(类型为Pie,每种类型的最大值);
花生总蛋白++;
}
typeOfPie=getPieType();
}
if(PIE.equalsIgnoreCase(“q”)的类型){
int totalPies=calcTotalPies(苹果总、蓝莓总、花生总);
双倍利润=calcProfit(苹果总公司、蓝莓总公司、花生总公司);
打印报告(totalPies、appleTotal、blueberryTotal、peanutTotal、profit);
}
}
公共静态字符串getPieType(){
字符串类型;
做{
试一试{
pieType=JOptionPane.showInputDialog(“输入饼图类型:”);
}         
捕获(数字格式){
pieType=“”;
}      
如果(!pieType.equalsIgnoreCase(“苹果”)和&!pieType.equalsIgnoreCase(“蓝莓”)&&
!pieType.equalsIgnoreCase(“花生”)和&!pieType.equalsIgnoreCase(“q”){
showMessageDialog(null,“仅输入‘苹果’、‘蓝莓’、‘花生’或‘q’”);
}     
}而(!pieType.equalsIgnoreCase(“苹果”)和&!pieType.equalsIgnoreCase(“蓝莓”)&&
!pieType.equalsIgnoreCase(“花生”)和&!pieType.equalsIgnoreCase(“q”);
返回类型;
}
公共静态字符串[]fillApple(字符串类型为Pie,每种类型的int MAX_){
String[]applarray=新字符串[MAX_PER_TYPE];
for(int i=0;i0){
JOptionPane.showMessageDialog(null,
“饼图报告\n\n”+
“总馅饼数:”+总馅饼数+
“\n苹果派总计:”+appleTotal+
“\n蓝莓派总计:”+蓝莓总计+
“\n花生酱馅饼总数:”+花生酱总数+
“\n总利润:$”+String.format(“%.2f”,利润));
}
否则{
showMessageDialog(null,“享受你的假期”);
}
}
}

您并没有真正使用
字符串[]
s
applarray
blueberryArray
peanutArray
——它们是用各自的方法创建的,但不在其他任何地方使用。为了计算利润,你(正确地)只是全部变量

而不是

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++;
    }
}

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

您并没有真正使用
字符串[]
s
applarray
蓝莓鱼
花生阵列
——它们是在各自的方法中创建的,但不会在其他任何地方使用。为了计算利润,你(正确地)只是全部变量

而不是

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++;
    }
}

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

每次添加饼图阵列时,都要重新定义它们

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;
}
publicstaticstring[]fillApple(stringtypeofpie,int MAX\u PER\u类型){
String[]applarray=新字符串[MAX_PER_TYPE];
for(int i=0;i

每次调用此方法时,都会生成一个新的“applarray”。如果希望它在调用此方法之间保持不变,请将applarray声明为循环外部的私有静态,并引用它。

每次添加饼图数组时,都要重新声明它们

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;
}
publicstaticstring[]fillApple(stringtypeofpie,int MAX\u PER\u类型){
String[]applarray=新字符串[MAX_PER_TYPE];
for(int i=0;i