2017-04-25 6 views
-1

このプログラムでは、ユーザーが学生の名前とスコアを10回入力して平均と学生の名前を出力することができます平均。平均より大きい/小さいスコアを持つ生徒を出力するプログラムのポイントに達すると、すべての名前を一度だけ印刷するのではなく、繰り返します。私は間違って何をしていますか?ループを使わずに出力文を一度印刷するのに問題がある

ありがとうございます。java.util.Scannerをインポートしてください。

public class Grades{ 

    public static void main(String[] args){ 

    //create a keyboard representing the scanner 
     Scanner console = new Scanner(System.in); 

    //define variables 
     double [] score = new double[10]; 

     String [] name = new String[10]; 
     double average = 0.0, sum = 0.0, studentAverage = 0.0, highestScore = 0.0, lowestScore = 0.0; 


     for(int i= 0; i < score.length; i++){ 

     System.out.println("Enter the student's name: "); 
     name[i] = console.next(); 
     System.out.println("Enter the student's score: "); 
     score[i] = console.nextDouble(); 

     sum += score[i]; 

     }//end for loop 

     //calculate average 
     average = sum/score.length; 

     System.out.println("The average score is: " + average); 


     int highestIndex = 0; 

     for(int i = 1; i < score.length; i++){ 

     if(score[highestIndex] < score[i]){ 

      highestIndex = i; 

     } 

     if(score[i] < average){ 
      System.out.print("\nNames of students whose test scores are less than average: " + name[i]); 
     } 

     if(score[i] >= average){ 
      System.out.print("\nNames of students whose test scores are greater than or equal to average: " + name[i]); 
     } 


     }//end for loop 

    }//end main 

}//end clas 

`

+0

さて、あなたは 'System.out.print("学生の名前... ")' _inside_ a loopを呼び出しているので、 "ループでそうしています"。おそらくリストに名前を集めてから印刷したいと思うかもしれません(また、ループを使いたいかもしれないリスト要素を出力するには 'System.out.print(" names students of students ...: ") ; for(String name:list){/ *ここに名前を印刷する* /} '。 – Thomas

+0

あなたの問題が何かと言い直す必要があると思います。 –

答えて

0

は、そのようなあなたのループを変更します。

System.out.print("Names of students whose test scores are less than average: "); 
for(int i = 1; i < score.length; i++){ 
    if(score[i] < average){ 
     System.out.print(name[i]); 
    } 
} 

System.out.print("Names of students whose test scores are greater than or equal to average: "); 
for(int i = 1; i < score.length; i++){ 
    if(score[i] >= average){ 
     System.out.print(name[i]); 
    } 
} 

をあなたの現在のコードを使用すると、各ループの繰り返しで、あなたのテキストを含む同じ行をプリントアウト。変更されたコードを使用すると、それを1回だけ印刷し、その後に名前を付けるだけです。

+0

ありがとう – user7613788

関連する問題