Java计算问题

时间:2016-04-22 12:27:41

标签: java arrays eclipse loops for-loop

这里有很长的问题,只是免责声明。

我正在尝试用Eclipse编写平均分数“计算器”。这是一项大学任务,“情景”是一个虚构的冬季奥运会,专注于单板滑雪。每位运动员有2次跑步,每位7位评委给出10分。 我正在使用Eclipse和WindowBuilder来构建GUI。分数输入7个名为txtRun1_0的单个文本字段; txtRun1_1; txtRun1_2;等到txtRun1_6。如下面的代码所示,我将字符串存储在一个数组中,然后将每个字符串解析为另一个同样大小的数组中的double,以获得双精度数。然后我继续计算。

令人困惑的部分是,下面的代码只是适用于GUI;我最初写它是为了使用控制台,它完美地工作。我还会包含原始代码。正如您将看到的,代码的主要计算部分几乎完全相同,除了变量名称中的几个非常小的变化。

编辑:该程序的问题是:如果我输入1,2,3,4,5,6和7作为分数,它将得分的平均值返回为0.0,并且最高得分最低的是7.0。

该计划必须遵守以下标准:

  • 从计算中截断最高和最低分数
  • 阵列是必须的

我已对所有标签和变量进行了双重检查和三重检查,但我没有发现任何问题。

这是我的GUI程序代码(对于运行1):

            //Start Variables Declaration
            double [] daRun1 = new double[7]; //Run1 Array
            double [] daRun2 = new double[7]; //Run2 Array
            String [] saRun1 = new String[7]; //Run1 String Array
            String [] saRun2 = new String[7]; //Run2 String Array
            int di1; //daRun1 index integer
            int di2; //daRun2 index integer
            int si1; //saRun1 index integer
            int si2; //saRun2 index integer

            //End Variables Declaration

            ///////////////
            //Run 1 Start//
            ////////////////////////////////////////////////////////
            //Get values from JTextFields (txtRun1_0 to txtRun1_6)//
            //and enter them in to their respective arrays//////////
            //Run 1 Scores//////////////////////////////////
            ///////////////
            saRun1[0] = txtRun1_0.getText();
            saRun1[1] = txtRun1_1.getText();
            saRun1[2] = txtRun1_2.getText();
            saRun1[3] = txtRun1_3.getText();
            saRun1[4] = txtRun1_4.getText();
            saRun1[5] = txtRun1_5.getText();
            saRun1[6] = txtRun1_6.getText();

            //Parses string values of saRun1 to integers
            //and enters them into daRun1 (array of doubles)
            //to make calculation of average scores possible
            for (di1=0; di1<daRun1.length; di1++)
            {
                for (si1=0; si1<saRun1.length; si1++)
                {
                    daRun1[di1] = Double.parseDouble(saRun1[si1]);
                }
            }

            //Finds the maximum and minimum scores
            //for Run 1, ready for truncating them during
            //average score calculation
            double min1 = daRun1[0];
            double max1 = daRun1[0];

            for (di1=0; di1<daRun1.length; di1++)
            {
                if (daRun1[di1] > max1)
                    {max1 = daRun1[di1];}

                if (daRun1[di1] < min1)
                    {min1 = daRun1[di1];}
            }

                //These println statements are there to help me debug the code; the result of the calculation is allocated to a label further down in the code.
                System.out.println("The maximum score for Run 1 is: " + max1 + "\nThe minimum score for Run 1 is: " + min1); 

            //Calculates the average score 
            //for Run 1 (not including the maximum and minimum scores)
            double sum1 = 0.0;
            double avg1 = 0.0;

            for (di1=0 ; di1<daRun1.length ; di1++)
            {
                if(daRun1[di1] == max1 || daRun1[di1] == min1)
                    continue;
                sum1 += daRun1[di1];
            }
            avg1 = sum1 / (daRun1.length-2);


            //Output to lblAvgRun1
            lblAvgRun1.setText(Double.toString(avg1));
            System.out.println(avg1);

这是我的控制台版本代码:

    double [] daRun1 = new double[7]; //Run1 Array
    double [] daRun2 = new double[7]; //Run2 Array
    int i1; //daRun1 index integer
    int i2; //daRun2 index integer


    //Initializes console input
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));


    ////////////////
    //Run 1 Start///
    ////////////////

    System.out.println("Enter the 7 scores for run 1:");

    //Run 1 Data Entry below
    for (i1=0; i1<daRun1.length; i1++)
    {
        daRun1[i1] = Double.parseDouble(br.readLine());
    }

    //Printing Array Elements - Run 1
    for (i1=0; i1<daRun1.length; i1++)
    {
        System.out.println(i1+1 + " " + daRun1[i1]);
    }

    //Find maximum and minimum scores - Run 1
    double min1 = daRun1[0];
    double max1 = daRun1[0];

    for (i1=0; i1<daRun1.length; i1++)
    {
        if (daRun1[i1] > max1)
            {max1 = daRun1[i1];}

        if (daRun1[i1] < min1)
            {min1 = daRun1[i1];}
    }
    System.out.println("The maximum score for Run 1 is: " + max1 + "\nThe minimum score for Run 1 is: " + min1);

    //Calculate the average score - Run 1
    double sum1 = 0.0;
    double avg1 = 0.0;

    for (i1=0 ; i1<daRun1.length ; i1++)
    {
        if(daRun1[i1] == max1 || daRun1[i1] == min1)
            continue;
        sum1 += daRun1[i1];
    }
    avg1 = sum1 / (daRun1.length-2);

    System.out.println("The average score for Run 1 is: " + avg1);
    //End Run 1//////////////////////////////////////////////////////////////////////////////////////////////////////////////

1 个答案:

答案 0 :(得分:0)

尝试更改:

for (si1=0; si1<saRun1.length; si1++)
{
    daRun1[di1] = Double.parseDouble(saRun1[si1]);
}

要:

for (si1=0; si1<saRun1.length; si1++)
{
    daRun1[si1] = Double.parseDouble(saRun1[si1]);
}

每次循环时,你只是覆盖相同的数组元素......

此外,我不确定为什么你有外环,因为它不是一个多维数组。内环应该足够......