使用下面的代码,变量(double)dAvgRun1,dAvgRun2(以及变量dBest)在输出到GUI中的标签时返回为NaN。
我的代码中有什么问题让我的双打NaN?
double [][] daRun = new double[2][7];
//Sets Athlete Name and Nationality labels to selected athlete in JList
jLabel_Athlete.setText(vecAthlete.get(jList_Athlete.getSelectedIndex()).sName);
jLabel_Country.setText(vecAthlete.get(jList_Athlete.getSelectedIndex()).sCountry);
//Returns all scores for selected athlete
jTextField_Run1_0.setText(vecAthlete.get(jList_Athlete.getSelectedIndex()).saTempRun1[0]);
jTextField_Run1_1.setText(vecAthlete.get(jList_Athlete.getSelectedIndex()).saTempRun1[1]);
jTextField_Run1_2.setText(vecAthlete.get(jList_Athlete.getSelectedIndex()).saTempRun1[2]);
jTextField_Run1_3.setText(vecAthlete.get(jList_Athlete.getSelectedIndex()).saTempRun1[3]);
jTextField_Run1_4.setText(vecAthlete.get(jList_Athlete.getSelectedIndex()).saTempRun1[4]);
jTextField_Run1_5.setText(vecAthlete.get(jList_Athlete.getSelectedIndex()).saTempRun1[5]);
jTextField_Run1_6.setText(vecAthlete.get(jList_Athlete.getSelectedIndex()).saTempRun1[6]);
jTextField_Run2_0.setText(vecAthlete.get(jList_Athlete.getSelectedIndex()).saTempRun2[0]);
jTextField_Run2_1.setText(vecAthlete.get(jList_Athlete.getSelectedIndex()).saTempRun2[1]);
jTextField_Run2_2.setText(vecAthlete.get(jList_Athlete.getSelectedIndex()).saTempRun2[2]);
jTextField_Run2_3.setText(vecAthlete.get(jList_Athlete.getSelectedIndex()).saTempRun2[3]);
jTextField_Run2_4.setText(vecAthlete.get(jList_Athlete.getSelectedIndex()).saTempRun2[4]);
jTextField_Run2_5.setText(vecAthlete.get(jList_Athlete.getSelectedIndex()).saTempRun2[5]);
jTextField_Run2_6.setText(vecAthlete.get(jList_Athlete.getSelectedIndex()).saTempRun2[6]);
//Parse all scores from string to double
daRun[0][0] = Double.parseDouble(jTextField_Run1_0.getText());
daRun[0][1] = Double.parseDouble(jTextField_Run1_1.getText());
daRun[0][2] = Double.parseDouble(jTextField_Run1_2.getText());
daRun[0][3] = Double.parseDouble(jTextField_Run1_3.getText());
daRun[0][4] = Double.parseDouble(jTextField_Run1_4.getText());
daRun[0][5] = Double.parseDouble(jTextField_Run1_5.getText());
daRun[0][6] = Double.parseDouble(jTextField_Run1_6.getText());
daRun[1][0] = Double.parseDouble(jTextField_Run2_0.getText());
daRun[1][1] = Double.parseDouble(jTextField_Run2_1.getText());
daRun[1][2] = Double.parseDouble(jTextField_Run2_2.getText());
daRun[1][3] = Double.parseDouble(jTextField_Run2_3.getText());
daRun[1][4] = Double.parseDouble(jTextField_Run2_4.getText());
daRun[1][5] = Double.parseDouble(jTextField_Run2_5.getText());
daRun[1][6] = Double.parseDouble(jTextField_Run2_6.getText());
//Finds maximum and minimum scores for both runs
int z;
double dMinScoreRun1 = 10, dMaxScoreRun1 = 0;
double dMinScoreRun2 = 10, dMaxScoreRun2 = 0;
for (z=0; z<daRun.length; z++)
{
if (daRun[0][z] > dMaxScoreRun1)
{dMaxScoreRun1 = daRun[0][z];}
if (daRun[0][z] < dMinScoreRun1)
{dMinScoreRun1 = daRun[0][z];}
if (daRun[1][z] > dMaxScoreRun2)
{dMaxScoreRun2 = daRun[1][z];}
if (daRun[1][z] < dMinScoreRun2)
{dMinScoreRun2 = daRun[1][z];}
}
//Calculates average scores
double dTotalRun1 = 0, dTotalRun2 = 0;
double dAvgRun1 = 0, dAvgRun2 = 0;
//Run 1
for (z=0; z<daRun.length; z++)
{
if(daRun[0][z] == dMaxScoreRun1 || daRun[0][z] == dMinScoreRun1)
continue;
dTotalRun1 += daRun[0][z];
}
dAvgRun1 = dTotalRun1 / (daRun.length-2);
//Run 2
for (z=0; z<daRun.length; z++)
{
if(daRun[1][z] == dMaxScoreRun2 || daRun[1][z] == dMinScoreRun2)
continue;
dTotalRun2 += daRun[1][z];
}
dAvgRun2 = dTotalRun2 / (daRun.length-2);
//Displays averages for both runs
jLabel_Average1.setText(Double.toString(dAvgRun1));
jLabel_Average2.setText(Double.toString(dAvgRun2));
//Compare both scores to find the best score.
double dBest = dAvgRun1;
if (dAvgRun2 > dAvgRun1)
{
dBest = dAvgRun2;
}
//Sets text of lblBestScore to the highest of the two scores
jLabel_Best.setText(Double.toString(dBest));
答案 0 :(得分:3)
double [][] daRun = new double[2][7];
你使用
dAvgRun1 = dTotalRun1 / (daRun.length-2);
whic将执行dTotalRun1/0
。你可能想要
dAvgRun1 = dTotalRun1 / (daRun[0].length-2);
但这也存在问题。如果有多个值等于最小值或最大值,该怎么办?你应该数:
int cnt = 0;
for (z=0; z<daRun.length; z++){
if(daRun[0][z] == dMaxScoreRun1 || daRun[0][z] == dMinScoreRun1)
continue;
dTotalRun1 += daRun[0][z];
cnt++;
}
if( cnt != 0 ){
dAvgRun1 = dTotalRun1 / cnt;
} else {
// ? not sure what to use here
}
答案 1 :(得分:0)
我认为问题在于:for (z=0; z<daRun.length; z++)
daRun.length将返回2而不是7,正如您所料。