このプログラムでは、ユーザーが学生の名前とスコアを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
`
さて、あなたは 'System.out.print("学生の名前... ")' _inside_ a loopを呼び出しているので、 "ループでそうしています"。おそらくリストに名前を集めてから印刷したいと思うかもしれません(また、ループを使いたいかもしれないリスト要素を出力するには 'System.out.print(" names students of students ...: ") ; for(String name:list){/ *ここに名前を印刷する* /} '。 – Thomas
あなたの問題が何かと言い直す必要があると思います。 –