亲爱的能够解决这个问题的人,我真的很感谢你们的好意! 我是一个新的编程学习者(不到1年)
我尝试使用 toSting() 但它也会打印出空索引,我如何摆脱这些空索引并只显示我想要的分数而不使用 Stream 这是我的代码:
public class ArrayStream4 {
public static void main(String[] args) {
int [] scores = {100, 50, 40, 70, 90};
// Question: Please calculate the following: How many individuals have achieved a "passing" grade(i>=60) and print out the grade? The total score? The average? The highest score? The lowest score?
int total = 0;
int max = scores[0];
int min = scores[0];
int passCount = 0;
int [] newPassArray = new int[scores.length];
int passingIndex = 0;
for(int score : scores){
total += score;
if(score > max){
max = score;
}
if (score < min){
min = score;
}
if(score >= 60){
passCount ++;
newPassArray[passingIndex] = score;
passingIndex ++;
}
}
for (int i = 0; i < passingIndex; i++) {
System.out.print(newPassArray[i]);
}
System.out.println();
System.out.println("PASS grade: " + Arrays.toString(newPassArray));
System.out.printf("Total grade: %d\n", total);
System.out.printf("Min grade: %d\n", min);
System.out.printf("Max grade: %d\n", max);
System.out.printf("PASSCount: %d\n", passCount);
}
}
问题解决了!!
public class ArrayStream4 {
public static void main(String[] args) {
int[] scores = {100, 50, 40, 70, 90};
int total = 0;
int max = scores[0];
int min = scores[0];
int passCount = 0;
List<Integer> newPassList = new ArrayList<>();
for (int score : scores) {
total += score;
if (score > max) {
max = score;
}
if (score < min) {
min = score;
}
if (score >= 60) {
newPassList.add(score);
passCount++;
}
}
System.out.println("及格分數: " + newPassList);
System.out.printf("總和: %d\n", total);
System.out.printf("最小值: %d\n", min);
System.out.printf("最大值: %d\n", max);
System.out.printf("及格: %d\n", passCount);
}
}