我的if块出了什么问题?

时间:2013-12-11 02:29:57

标签: java arrays swing if-statement

除此之外,我程序中的所有内容都有效。我不知道问题是什么。如果用户输入超过25个等级,我需要程序吐出错误消息。这是我的代码

package my.meancalculator;

import javax.swing.JOptionPane;
import javax.swing.JFrame;
import java.text.DecimalFormat;


public class MeanCalcUI extends javax.swing.JFrame {

private final DecimalFormat formatter = new DecimalFormat("#0.0");
private double gradeAverage;
private double standardDeviation;
JFrame frame = new JFrame();
private double[] gradeArray = new double[25];
private int numberOfGradesInput = 0;


public MeanCalcUI() {
    initComponents();
    setLocationRelativeTo(null);
}

public double getAverage(double[] gradeArray, int numberOfGradesInput) {

    double sum = 0;

    for (int i = 0; i < numberOfGradesInput; i++) {
        sum = sum + gradeArray[i];
    }

    return (sum / numberOfGradesInput);
}

public double getStdDev(double[] gradeArray, int numberOfGradesInput, double average) {

    double sum = 0;

    for (int i = 0; i < numberOfGradesInput; i++) {
        sum = sum + Math.pow((gradeArray[i] - average), 2);
    }

    return Math.sqrt(sum / numberOfGradesInput);
}


private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {                                        
    System.exit(0);
}                                       

private void btnEnterGradesActionPerformed(java.awt.event.ActionEvent evt) {                                               

    if (numberOfGradesInput > 25) {
        // We've already finished entering the max # of grades
        JOptionPane.showMessageDialog(frame,
                "You can only input 25 grades!",
                "Too much data!",
                JOptionPane.ERROR_MESSAGE);
        return;         
    }
    do {
        String gradeInput = JOptionPane.showInputDialog(frame,
                "Enter Grade",
                "Enter Grade",
                JOptionPane.PLAIN_MESSAGE);

        // When we receive empty/null input, we're done entering grades
        if (gradeInput == null || gradeInput.length() == 0) {
            break;
        }

        double gradeValue = 0; // Set to avoid 'may be unset' compiler error
        try {
            gradeValue = Double.parseDouble(gradeInput);
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(frame,
                    "Your input must be numeric!",
                    "Bad Data!",
                    JOptionPane.ERROR_MESSAGE);
            continue; // start over again
        }

        // Put the grade into the array and update the number of grades entered
        gradeArray[numberOfGradesInput] = gradeValue;
        numberOfGradesInput++;

        // Add to the grade total
        txtNumGrades.setText(formatter.format(numberOfGradesInput));

        //use the getAverage method to get the average of the grades
        gradeAverage = getAverage(gradeArray, numberOfGradesInput);
        txtMean.setText(formatter.format(gradeAverage));

        //use the getStdDev method to get the standard deviation
        standardDeviation = getStdDev(gradeArray, numberOfGradesInput, gradeAverage);
        txtStdDeviation.setText(formatter.format(standardDeviation));
    } while (numberOfGradesInput < 25);



}  

我使用了我的整个代码,以防它是因为它导致了这个问题。每次我运行程序时,我的窗口数超过25倍,它会要求用户输入关闭,并且不会弹出错误消息。我有什么不对吗?

3 个答案:

答案 0 :(得分:0)

我认为您需要将条件更改为>=,因为您的do / while要求25个等级,之后您的条件仍然是假的并且您将再次进入循环并且将超出范围异常

if (numberOfGradesInput >= 25)

答案 1 :(得分:0)

认为你需要将你的条件改为&gt; =,因为你的do / while要求25个等级,之后你的条件仍然是假的,你将再次进入循环并且将超出界限异常

if (numberOfGradesInput >= 25)

当您的按钮操作被触发时,它应检查它是否大于或等于25.在if语句和while循环之间,您有一个未涵盖的用例。

如果成绩是25,怎么办?使用您编写的代码,它将跳过您的if语句并输入您的do-while循环并添加“26年级”(实际上会导致IndexOutOfBoundsException而不是打印出错误对话框。

答案 2 :(得分:0)

问题出在你的病情上,并且做到了。我尝试对代码进行最少的修改以使其正常工作。

        do {
            if (numberOfGradesInput >= 25) {
                // We've already finished entering the max # of grades
                JOptionPane.showMessageDialog(frame,
                        "You can only input 25 grades!",
                        "Too much data!",
                        JOptionPane.ERROR_MESSAGE);
                return;
            }
            else{
                //....
             }
         } while (numberOfGradesInput < 26);