InputDialogue Joptionpane 有 -1
InputDialogue Joptionpane has -1
我的程序工作正常,但是当它打开 JOptionPane.showInputDialog 输入成绩时,框的文本字段中有一个突出显示的 -1。我很无能,搜索结果一无所获。感谢您的宝贵时间!
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;
)
改为
我的程序工作正常,但是当它打开 JOptionPane.showInputDialog 输入成绩时,框的文本字段中有一个突出显示的 -1。我很无能,搜索结果一无所获。感谢您的宝贵时间!
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;
)
改为