为什么这个方法/数组不会为频率

时间:2016-05-01 18:25:45

标签: java arrays

为此,我想打印出选择每种类型汽车的次数的频率...但是,当我运行程序时,每次我选择本田时,它打印出0(频率) 。 最好在每个节目结束时打印出所有频率的表格,但是我不知道如何到达那里。

public static void main(String[] args) {
    int prompt;
    int[] carsValues = new int[5];//didnt want to use 0

    do{
        prompt = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter"
            + "\n"
            + "1 For Honda"
            + "\n"
            + "2 For Toyota"
            + "\n"
            + "3 For Ford"
            + "\n"
            + "4 For Chevrolet"
            + "\n"
            + "5 For Kia"
            + "\n"
            + "6 To Quit"));

        if (prompt == 1)
        {     
            display(carsValues);
            int n = 1;
            carsValues[n]++;
            display(carsValues);
        };
        if (prompt == 2)
        {
            display(carsValues);
            int n = 2;
            carsValues[n]++;
            display(carsValues);
        };

        if (prompt == 3)
        {
            display(carsValues);
            int n = 3;
            carsValues[n]++;
            display(carsValues);
        };
        if (prompt == 4)
        {
            display(carsValues);
            int n = 4;
            carsValues[n]++;
            display(carsValues);
        };
        if (prompt ==5)
        {
            display(carsValues);
            int n = 5;
            carsValues[n]++;
            display(carsValues);
        }
        if (prompt >= 7)
        {
        JOptionPane.showMessageDialog(null, "Unrecognizable Command"
                    + "\n"
                    + "Error: Entered Option Is Greater Than The Choice of 5"
                    + "\n"
                    + "Try Again"
                    + "\n");
        };
        if (prompt <= 0)//child proofing
        {
        JOptionPane.showMessageDialog(null, "Unrecognizable Command"
                    + "\n"
                    + "Error: Entered Option Is A 0 Or A Negative"
                    + "\n"
                    + "Try Again"
                    + "\n");
        };         
    }while(prompt!= 6);
}
public static void display(int[] input){
    try{
        int miles, gallons, mpg;

        miles = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter Miles Driven "));
        if (miles <= -1){
            JOptionPane.showMessageDialog(null,"Input Is Negative"
                    + "\n"
                    + "Try Again");
        miles = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter Miles Driven ")); 
        }
        gallons = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter Gallons Used "));
        if (gallons <= -1){
            JOptionPane.showMessageDialog(null,"Input Is Negative"
                    + "\n"
                    + "Try Again");
        gallons = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter Gallons Used ")); 
        }


            mpg = miles/gallons;
            JOptionPane.showMessageDialog(null, String.format("MPG Turns Out To Be %n" + mpg));

    }catch(ArithmeticException mathError){
            JOptionPane.showMessageDialog(null, "Division by Zero"
                + "\n"
                + "Can't Do That");   
        }
    for(int counter = 0; counter < input.length; counter++){
        JOptionPane.showMessageDialog(null, "Amount of Times Chosen"
                + "\n"+(input[counter]));
        break;// bad idea
    }
}

}

2 个答案:

答案 0 :(得分:1)

因为carsValues[0]永远不会增加,但每次只打印carsValues[0]。查看break循环中的for

for(int counter = 0; counter < input.length; counter++){
        JOptionPane.showMessageDialog(null, "Amount of Times Chosen"
                + "\n"+(input[counter]));
        break;// bad idea
}

答案 1 :(得分:0)

在您的示例中,最后一个循环:

for(int counter = 0; counter < input.length; counter++){
    JOptionPane.showMessageDialog(null, "Amount of Times Chosen"
            + "\n"+(input[counter]));
    break;// bad idea

循环每次都在0之后中断,并且从不使用carsValues [0]。所以你的结果总是0。

这是一种有效的方法。它可以根据您的技能水平进行重构​​。

import javax.swing.JOptionPane;
public class HelloWorld
{
  public static void main(String[] args) {
      int prompt;
      int[] carsValues = new int[6];//didnt want to use 0
      do{
          prompt = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter"
              + "\n"
              + "1 For Honda"
              + "\n"
              + "2 For Toyota"
              + "\n"
              + "3 For Ford"
              + "\n"
              + "4 For Chevrolet"
              + "\n"
              + "5 For Kia"
              + "\n"
              + "6 To Quit"));
          if(prompt < 0 || prompt > 6){
            JOptionPane.showMessageDialog(null, "Unrecognizable Command"
                                                + "\n"
                                                + "Error: Entered Option must be between 1 and 5 inclusive"
                                                + "\n"
                                                + "Try Again"
                                                + "\n");
          }
          else{
              int n = prompt;
              carsValues[n]++;
              display(carsValues);
          }
      }while(prompt!= 6);
  }
  public static void display(int[] input){
      try{
          int miles, gallons, mpg;
          miles = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter Miles Driven "));
          if (miles <= -1){
              JOptionPane.showMessageDialog(null,"Input Is Negative"
                      + "\n"
                      + "Try Again");
          miles = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter Miles Driven ")); 
          }
          gallons = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter Gallons Used "));
          if (gallons <= -1){
              JOptionPane.showMessageDialog(null,"Input Is Negative"
                      + "\n"
                      + "Try Again");
          gallons = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter Gallons Used ")); 
          }
              mpg = miles/gallons;
              JOptionPane.showMessageDialog(null, String.format("MPG Turns Out To Be %n" + mpg));
      }catch(ArithmeticException mathError){
              JOptionPane.showMessageDialog(null, "Division by Zero"
                  + "\n"
                  + "Can't Do That");   
          }
          JOptionPane.showMessageDialog(null, "Amount of Times Chosen"
                + "\n"
              + "Honda: " + input[1]
              + "\n"
              + "Toyota: " + input[2]
              + "\n"
              + "Ford: "  + input[3]
              + "\n"
              + "Chevrolet: " + input[4]
              + "\n"
              + "Kia: " + input[5]
              + "\n");
  }
}