2017-10-15 13 views
-3

私はいくつかの試験のスコアを入力して全体的に最高と最低のスコアを入力するよう質問しています。 (質問を強調した。) The questionJavaで最高のスコア

float sum = 0, score1, score2 = 0, score3 = 0; 

    do 
    { 
     System.out.println("Enter a score [negative score to quit]: "); 

     Scanner Score1 = new Scanner(System.in); 
     score1 = Score1.nextFloat(); 


     if (score1>score2) 
      { 
       max = score1; 
       min = score2; 
      } 

     else if(score1<score2) 
      { 
       max = score2; 
       min = score1; 
      } 

     else{ 
       score3 = score2;      
      } 

     } 


    } 
    while (score1>=0); 

    System.out.printf("Minimun Score = %.0f\n", min); 
    System.out.printf("Maximum Score = %.0f\n", max); 



    Enter a score [negative score to quit]: 
    99 
    Enter a score [negative score to quit]: 
    1 
    Enter a score [negative score to quit]: 
    6 
    Enter a score [negative score to quit]: 
    5 
    Enter a score [negative score to quit]: 
    -1 

    Minimum Score = 5 
    Maximum Score = 6 

    BUILD SUCCESSFUL (total time: 7 seconds) 

問題はそれだけで私が入力された最新の2得点を比較しています。 どうすれば修正できますか?

+4

3つの変数がありますが、ユーザーは5つの値を入力します。おそらく1秒間これについて考えます。次に、配列に関するいくつかの研究を行います。それはあなたがここで使用すべきものですから。 – GhostCat

+1

これは、毎回新しいスキャナオブジェクトを作成するためです。 –

+1

スコア2を取り除き、常にあなたのスコアを1分と最大と比較してください。 – mlecz

答えて

0

論理if-elseラダーが間違っています。

あなたは便利な次見つけることがあります。

class Test 
{ 
    public static void main (String[] args) throws java.lang.Exception 
    { 
     try(Scanner Score1 = new Scanner(System.in)) 
     { 
      //Marks are in range of 0-100 
      float curNum, max = -1, min = 101, sum = 0, sumsq = 0; 
      boolean flg = true; 

      while(flg) 
      { 
       System.out.println("Enter a score [negative score to quit]: "); 
       curNum = Score1.nextFloat(); 
       if(curNum<0) break; 
       if(max < curNum) max = curNum; 
       if(min > curNum) min = curNum; 
       sum += curNum; 
       sumsq += (curNum*curNum); 
      } 
      System.out.printf("Minimun Score = %.0f\n", min); 
      System.out.printf("Maximum Score = %.0f\n", max); 
     } 

    } 
} 

あなたはそれがDO-ながら、ロジックは上記のコードでは動作しないことがありhere

0

作業見つけることができます。下記の作業コードを参照してください。

float sum = 0, score = 0; 
    float min = 0, max = 0, ave = 0; 
    Scanner stdInput = new Scanner(System.in); 

    System.out.println("Enter the scores: "); 
    min = max = score = stdInput.nextFloat(); 
    do { 
     System.out.println("Enter a score [negative score to quit]: "); 
     if (score > max) { 
      max = score; 
     }else if (score < min) { 
      min = score; 
     }   
     sum += score; 
     // Read the input here so that, the code does not process the negative input. 
     score = stdInput.nextFloat(); 
    } while (score >= 0); 

    System.out.println(min); 
    System.out.println(max); 
+0

はい..何か問題が見つかった場合は教えてください –

+0

私はすでに元の投稿にも指定されていると信じています。 –

関連する問題