2017-11-27 9 views
-1

私はJavaクラスに問題があります。最初にここにプログラムへの私の指示がありますので、私がしようとしていることを理解してください。Java:最高と最低のスコアを見つける

ユーザーが999を入力するまで、任意の数のテストスコアを入力できるアプリケーションを作成します。 0以上100未満の場合は、適切なメッセージを表示し、スコアを使用しないでください。スコアが入力された後、入力されたスコアの数、ハイスコア、最小スコア、および算術平均を表示します。

---間違ったことがいくつかあります。私のif文は、1または999に等しくなければ答えを検証していません。最初に動作しますが、答えを間違って入力すると、ユーザに再入力を促す代わりにwhile文に移動します数字1または999。 ---次に、私は最低のスコアを得ることはできませんが、最高の作品です。ここで

は私のコードです:

package chp6homework; 
import java.util.Scanner; 
public class TestScoreStatistics { 

    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 
     int testScore; 
     int totalTests = 0; 
     int totalSum = 0; 
     int answer; 
     double average = 0; 
     int hightScore = 0; 
     int lowestScore = 0; 

     // Prompts user for input 
     System.out.print("Enter 1 to input a score.\n"); 
     System.out.print("Enter 999 to get results and exit. >> "); 
     answer = input.nextInt(); 

     if(answer != 1 && answer != 999) 
     { 
      System.out.print("Invaild number!\n"); 
      System.out.print("Enter 1 to input a score.\n"); 
      System.out.print("Enter 999 to get results and exit. >> "); 
      answer = input.nextInt(); 
     } 

     while(answer == 1) 
     { 
      System.out.print("Please enter a test score >> "); 
      testScore = input.nextInt(); 

      if(testScore < 0 || testScore > 100) 
      { 
       System.out.print("Invalid score! \n"); 
       System.out.print("please enter a score that is not less than zero and" + 
         " and not more than 100. >> "); 
       testScore = input.nextInt(); 
      } 
      totalSum = totalSum + testScore; 
      ++totalTests; 

      lowestScore = testScore; 

      if(testScore > hightScore) 
      { 
       hightScore = testScore; 
      } 
      else if(testScore < lowestScore) 
      { 
       lowestScore = testScore; 
      } 

      System.out.println("Your score was processed."); 
      System.out.print("Enter 1 to input a score.\n"); 
      System.out.print("Enter 999 to get results and exit.\n"); 
      answer = input.nextInt(); 

     } 

     average = totalTests * totalSum; 

     System.out.println("The number of tests: " + totalTests); 
     System.out.println("The total tests are: "+ totalSum); 
     System.out.println("The average of scores is: " + average); 
     System.out.println("Highest score: " + hightScore); 
     System.out.println("Lowest score: " + lowestScore); 
     } 
} 
+1

長いメソッドが悪いので、コードを複数のメソッドに分割します。すべての方法が1つのことを行います。私は読みやすく、作業しやすくなっています。 – rgv

+1

"私のif文は答えを検証していません[...]しかし、私が間違って答えると、" < - それは分岐条件の働きをします。 if文が自動的にループしたり、繰り返されることはありません。条件が満たされない場合、ifブロックはスキップされます。ループしたい場合は、自分でループを作成する必要があります。 –

+2

*いくつかのことが間違っています*。 「小さなプログラムをデバッグする方法」(https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)を見てください。それがあなたを助けることができるようだ。 – jrook

答えて

0

精神概要使用の抽象化を取得するには:

System.out.print("Enter a score or 999 to exit. >> "); 
while (read a number && number != 999) { 
    if (number out of valid range) { 
     System.out.print("A score must be between 1 and 100. >> "); 
    } else { 
     add score to statistics 
    } 
    System.out.print("Enter a score or 999 to exit. >> "); 
} 

これは私があなたに残りの部分を残して自宅の仕事です。

+0

私はあなたに私がやったことをしたと私はそれを考え出した! – Brittney87

関連する問題