gpt4 book ai didi

java - 输入对话 Joptionpane 有 -1

转载 作者:行者123 更新时间:2023-12-01 21:56:56 24 4
gpt4 key购买 nike

我的程序工作正常,但是当它打开 JOptionPane.showInputDialog 输入成绩时,框中的文本字段会突出显示 -1。我一无所知,搜索一无所获。感谢您的宝贵时间!

enter image description here

import javax.swing.JOptionPane;

public class MeanDeviCalc extends javax.swing.JFrame {

//set array size
private double[] gradeArray = new double[25];
//intialize number of grades
private int GradeTotal = 0;

/**
* Creates new form MeanDeviCalc
*/
public MeanDeviCalc() {
initComponents();
}

//get function for mean
public double getAverage(double[] gradeArray, int numElem) {
//intialize total with 0
double total = 0;
for (int i = 0; i < numElem; i++)
{
//add one for total when grade inputed
total=total+gradeArray[i];
}
//divide for mean
return (total/numElem);
}
//get function for standard deviation
public double getstddev(double[] gradeArray, int numElem, double average) {
//intialize total with 0
double total = 0;
for (int i = 0; i < numElem; i++)
{
//standard deviation
total = total + Math.pow((gradeArray[i] - average), 2);
}
return Math.sqrt(total / numElem);
}

boolean exitloop = false;

do {
String gradeInput = JOptionPane.showInputDialog(
"Enter Grade",
JOptionPane.PLAIN_MESSAGE);

// When we receive empty/null input, we're done entering grades
if (gradeInput == null || gradeInput.length() == 0)
exitloop=true;
if(!exitloop){
double gradeValue = 0;

if (GradeTotal == 25) {
// max array size check
JOptionPane.showMessageDialog(this,
"You've already entered the maximum of 25 grades.",
"Error",
JOptionPane.ERROR_MESSAGE);
return;
}

try {
gradeValue = Double.parseDouble(gradeInput);

} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this,
"Your input must be numeric!","Bad Data!",JOptionPane.ERROR_MESSAGE);

}
// Put grade in the array update total
gradeArray[GradeTotal] = gradeValue;
GradeTotal++;
// Add to grade total
txtGradeNumber.setText(Integer.toString(GradeTotal));

double gradeAverage = getAverage(gradeArray, GradeTotal);
txtMean.setText(Double.toString(gradeAverage));

double standardDeviation = getstddev(gradeArray, GradeTotal, gradeAverage);
txtStdDev.setText(Double.toString(standardDeviation));}
} while (GradeTotal < 25 && !exitloop) ;

最佳答案

因为您正在使用public static String showInputDialog(Component parentComponent, Object message, Object initialSelectionValue)并且 JOptionPane.PLAIN_MESSAGE 设置为 -1 (public static final int PLAIN_MESSAGE = -1;)

我认为您打算使用 public static String showInputDialog(Component parentComponent, Object message, String title, int messageType)

相反

关于java - 输入对话 Joptionpane 有 -1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34147613/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com