2016-04-22 13 views
-1

ここで長い質問は、免責事項です。Java計算の問題

私は平均スコア "電卓"をEclipseでコーディングしようとしています。それは大学の課題で、「シナリオ」はスノーボードに焦点を当てた架空の冬季オリンピックイベントです。各アスリートには2つのランがあり、7人の裁判官がそれぞれ10点のスコアを与えています。 私はEclipseを使ってWindowBuilderを使ってGUIを構築しています。スコアは、txtRun1_0という7つの個別のテキストフィールドに入力されます。 txtRun1_1; txtRun1_2; txtRun1_6に至ります。以下のコードで見られるように、配列に文字列を格納してから、各文字列を2倍の別の同じサイズの配列で2倍に解析しています。それから計算を続けます。

以下のコードは、単にGUIで動作するようになっている点です。私はもともとコンソールで動作するように書きましたが、完全に動作します。私は元のコードも含めます。お分かりのように、コードの主な計算部分は、変数名のわずかな変更を除けば、ほぼ同じです。

EDIT:プログラムの問題は次のとおりです。1,2,3,4,5,6、および7をスコアとして入力すると、スコアの平均が0.0、最も高いANDスコアの最低値は7.0です。

プログラムは以下の基準に準拠しています

  • 最高と最低のスコアは計算から切り捨てられ
  • 配列は、私は、二重にチェック、トリプルまし必見

です私のすべてのラベルと変数の名前をチェックしましたが、間違ったものは見つかりませんでした。ここで

は(ラン1)GUIプログラムのための私のコードです:

  //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////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
+2

正確にあなたの質問は何ですか? – Nadir

+1

あなたはそうです。私はこの問題を書き留めるのを忘れてしまった。 OPを編集しました。 – RThomP

+0

問題の内容を一文で説明してください。例えば、「GUIバージョンは、最高と最低スコアが切り捨てられていないようです。今私は何を探しているのか分からない。 – Kaa

答えて

0

は変更してみてください:

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

To:

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

あなたはちょうどそれが多次元配列ではありませんので、あなたがそこに外側のループを持っている理由も、私はわからないんだけど...

をごループするたびに同じ配列要素を上書きしています。内側のループは十分なはずです...

+0

ありがとう!私はそれが終わったと思う! これは、ネストされたループの最初の部分を削除できることを意味するはずです。 – RThomP

+0

@RThomPはい外部ループを取り除く必要はありません。 – brso05

+0

@RThomPは上のループを使用します。 – brso05