gpt4 book ai didi

Java 计算 double - NaN

转载 作者:行者123 更新时间:2023-11-29 04:46:47 26 4
gpt4 key购买 nike

使用下面的代码,变量( double )dAvgRun1、dAvgRun2(以及变量 dBest)在输出到 GUI 中的标签时返回为 NaN。

我在代码中做错了什么使我的 double 为 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));

最佳答案

double [][] daRun = new double[2][7];

你用

dAvgRun1 = dTotalRun1 / (daRun.length-2);

这将执行 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
}

关于Java 计算 double - NaN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36840253/

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