我正在自學Java,剛剛完成了數組的基礎知識學習,或者我認爲。我想創建一個能夠保證每個學生的成績和平均成績的班級成績單。我有一個for循環,要求10名學生中的每一個都要輸入4個測試成績。一旦我拿到這四個等級,我就取平均分。然後,我將學生的成績和平均數存入一個數組中,每個學生一個數組。我基本上用這5個元素創建了10個數組,每個學生使用for循環。我現在想從每個學生的數組中取第5個元素,即4個等級的平均值,並填充另一個稱爲平均值的數組,以便我可以執行其他計算。這可能與我的邏輯?我認爲我可以硬編碼10個陣列,每個學生1個像這樣:使用數組的元素填充另一個數組 - Java
double averages[] = {student1[4], student2[4], ..., student10[4]};
這不是一個糟糕的方式去解決這個問題嗎?任何建設性的幫助或指導將不勝感激。請不要發佈提供答案的代碼,因爲我不會從中學習。我只想要一個正確的方向提示。 :)
這裏是我的代碼,直到混亂的點:
import java.util.Scanner;
public class ClassAverages {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
double grade1 = 0.0, grade2 = 0.0, grade3 = 0.0, grade4 = 0.0, average = 0.0;
// get grades from each of the 10 students
for (int student = 1; student <= 3; student++) {
System.out.println("Student " + student);
System.out.println("---------\n");
System.out.print("Enter the first grade: ");
grade1 = keyboard.nextDouble();
while (grade1 < 0) { // input validation for grade 1
System.out.print("You entered a negative value for grade. Please re-enter a positive grade: ");
grade1 = keyboard.nextDouble();
}
System.out.print("Enter the second grade: ");
grade2 = keyboard.nextDouble();
while (grade2 < 0) { // input validation for grade 2
System.out.print("You entered a negative value for grade. Please re-enter a positive grade: ");
grade2 = keyboard.nextDouble();
}
System.out.print("Enter the third grade: ");
grade3 = keyboard.nextDouble();
while (grade3 < 0) { // input validation for grade 3
System.out.print("You entered a negative value for grade. Please re-enter a positive grade: ");
grade3 = keyboard.nextDouble();
}
System.out.print("Enter the fourth grade: ");
grade4 = keyboard.nextDouble();
System.out.println();
while (grade4 < 0) { // input validation for grade 4
System.out.print("You entered a negative value for grade. Please re-enter a positive grade: ");
grade4 = keyboard.nextDouble();
System.out.println();
}
// calculate the current student's average
average = (grade1 + grade2 + grade3 + grade4)/4;
// for each student, 1 to 10, create an array with their 4 grades and average
double studentX[] = { grade1, grade2, grade3, grade4, average };
System.out.println("SCORE 1\t\tSCORE 2\t\tSCORE 3\t\tSCORE 4\t\tAVERAGE");
System.out.print(studentX[0] + "\t\t");
System.out.print(studentX[1] + "\t\t");
System.out.print(studentX[2] + "\t\t");
System.out.print(studentX[3] + "\t\t");
System.out.print(studentX[4] + "\n");
System.out.println();
// I want to use each student's average for each corresponding element in the averages array
// create an array of all student's averages
// double averages[] = {student1average, student2average,...student10average} ???
}
}
}
輸出截至目前:
Student 1
---------
Enter the first grade: 100
Enter the second grade: 100
Enter the third grade: 100
Enter the fourth grade: 100
SCORE 1 SCORE 2 SCORE 3 SCORE 4 AVERAGE
100.0 100.0 100.0 100.0 100.0
Student 2
---------
Enter the first grade: 90
Enter the second grade: 90
Enter the third grade: 90
Enter the fourth grade: 80
SCORE 1 SCORE 2 SCORE 3 SCORE 4 AVERAGE
90.0 90.0 90.0 80.0 87.5
Student 3
---------
Enter the first grade: 100
Enter the second grade: 100
Enter the third grade: 90
Enter the fourth grade: 80
SCORE 1 SCORE 2 SCORE 3 SCORE 4 AVERAGE
100.0 100.0 90.0 80.0 92.5
我與階級結構是方式達成一致來解決這個問題。但是,因爲他剛剛學過數組,所以我不確定這是否屬於他所問的問題的範圍。我可以這樣寫: – KyleKW
in java 8它可以是: 'int [] averageArray = Stream.of(studentArray).map(student - > student.average).toArray();' – maxpovver
我同意這可能有點複雜,但我有意識到教compsci最好的方法之一是遇到一些困難的概念,並花費大量的時間來解決它。你不好意思將自己從可能困難的事情中解脫出來!不過,我完全同意這對於某人出發可能有些複雜。 – Jay