定義一個名為scores的整型陣列,為其分配100個元素的空間。程式接受用戶從鍵盤輸入的學生成績,用戶輸入-1時結束輸入(學生成績個數不超過100個),要求將這些成績保存到scores陣列中。最后,輸出所有成績的總分、平均分、最高分和最低分。
uj5u.com熱心網友回復:
用list不行嗎一定要用陣列?uj5u.com熱心網友回復:
import java.util.*;
/**
* @author zhaojinhui
* @date 2020/12/29 9:51
* @apiNote
*/
public class Score {
public static void main(String[] args) {
Integer[] scores = new Integer[100];
Scanner scanner = new Scanner(System.in);
int i = 1;
System.out.println("請輸入數字,按-1結束 ");
while(i <= 100){
i++;
int inNum = scanner.nextInt();
System.out.println("請輸入數字,按-1結束 ");
if(inNum == -1){
break;
}else{
scores[i] = inNum;
}
}
List<Integer> list = new ArrayList<>();
Collections.addAll(list,scores);
list.removeAll(Collections.singleton(null));
list.forEach(System.out::println);
DoubleSummaryStatistics statistics = list.stream().mapToDouble(Number::doubleValue).summaryStatistics();
System.out.println("最高分:" + statistics.getMax());
System.out.println("最低分:" + statistics.getMin());
System.out.println("平均分:" + Math.round(statistics.getAverage()));
System.out.println("總 分:" + statistics.getSum());
System.out.println("總個數:" + statistics.getCount());
}
}
uj5u.com熱心網友回復:
要用到陣列才行
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/242013.html
標籤:Eclipse
上一篇:idea運行springboot專案為什么Process finished with exit code 0
下一篇:幫幫忙
