【问题标题】:What is wrong with my if block?我的 if 块有什么问题?
【发布时间】:2013-12-11 02:29:56
【问题描述】:

除此之外,我的程序中的所有内容都可以正常工作。我不明白问题是什么。如果用户输入超过 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);



}  

我使用了我的整个代码,以防万一是 if 之外的东西导致了这种情况。每次我运行该程序时,它要求用户输入的窗口关闭了超过 25 次,并且没有弹出错误消息。我在这里做错了什么吗?

【问题讨论】:

  • if 在 do..while 循环之外
  • 不,不是这样。这是我尝试的第一件事,但它仍然没有弹出。

标签: java arrays swing if-statement


【解决方案1】:

我认为您需要将您的条件更改为&gt;=,因为您的 do/while 要求 25 分,之后您的条件仍然是错误的,您将再次进入循环并超出边界异常

if (numberOfGradesInput >= 25)

【讨论】:

  • 如果 numberOfGrades 大于或等于 25 就会成功。用户最多可以输入 25 个等级,但不能再输入了。
  • @Akira 这就是为什么你需要检查他们是否已经输入了 25if (numberOfGradesInput &gt; 25) 检查他们是否已输入超过 25 个
【解决方案2】:

认为您需要将您的条件更改为 >=,因为您的 do/while 要求 25 分,之后您的条件仍然为 false,您将再次进入循环并超出边界异常

if (numberOfGradesInput >= 25)

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

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

【讨论】:

  • 我把它改成了你说的那样,但并没有什么不同。它也没有添加第 26 个数字,它只是停止了。
【解决方案3】:

问题在于你的情况,然后做。我试图对您的代码进行最少的修改以使其正常工作。

        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);

【讨论】:

  • 在do-while循环中递增。
  • 我实际上做了numberOfGrades++...问题是当我输入25个等级时错误信息没有弹出,我必须再次点击动作按钮才能弹出跨度>
  • 我修改了我的评论,请看,如果您需要干净或正确的代码,请告诉我
猜你喜欢
  • 2013-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多