新聞中心
JAVA代碼問題!輸入5名學生的成績,并計算總成績,平均分,最高分,最低分
public static void main(String[] args) {
成都創(chuàng)新互聯(lián)公司作為成都網(wǎng)站建設公司,專注成都網(wǎng)站建設公司、網(wǎng)站設計,有關成都定制網(wǎng)站方案、改版、費用等問題,行業(yè)涉及砂巖浮雕等多個領域,已為上千家企業(yè)服務,得到了客戶的尊重與認可。
double scores[] = new double[5];
double total = 0;
double avg = 0;
double max = 0;
double min = 0;
int count=0;
String inputStr=null;
System.out.println("請輸入5名學生的成績:");
Scanner input = new Scanner(System.in);
while(count5){
try{
if(count 5){
System.out.println("請輸入第"+(count+1)+"個分數(shù):");
}
inputStr=input.nextLine();
scores[count++]=Double.valueOf(inputStr.trim());
}catch(Exception e){
if(inputStr!=null "exit".equals(inputStr.trim())){
System.out.println("您已成功結束程序");
System.exit(0);
}
System.out.println("若想結束請輸入:exit");
System.out.print("您輸入的分數(shù)不是數(shù)值類型,");
count--;
}
}
input.close();
Arrays.sort(scores);
min=scores[0];
max=scores[scores.length-1];
for(double score :scores){
total += score;
}
avg=total/scores.length;
System.out.println("總成績是" + total);
System.out.println("最高分是" + max);
System.out.println("最低分是" + min);
System.out.println("平均分是" + avg);
}
//-------------------------------------------------------------------------
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while(true){
Double[] scores = null;
double total = 0;
double avg = 0;
double max = 0;
double min = 0;
int count=1;
ListDouble inputScores=new ArrayListDouble();
String inputStr=null;
System.out.println("請輸入要統(tǒng)計學生的成績(理論上可以輸入無限個,前提是你有那么大的內存):");
while(true){
try{
System.out.println("請輸入第"+count+++"個分數(shù),或輸入ok進行計算,離開請輸入exit");
inputStr=input.nextLine();
inputScores.add((double)Double.valueOf(inputStr.trim()));
}catch(Exception e){
if(inputStr!=null "exit".equals(inputStr.trim().toLowerCase())){
System.out.println("您已成功結束程序");
input.close();
System.exit(0);
}
if(inputStr!=null "ok".equals(inputStr.trim().toLowerCase())){
break;
}
System.out.println("您輸入的分數(shù)不是數(shù)值類型,");
System.out.println("若想結束請輸入exit ,若想計算結果請輸入ok");
count--;
}
}
if(inputScores.size()==0){
System.out.println("您沒有輸入學生成績,無數(shù)據(jù)可統(tǒng)計,程序結束。");
return ;
}
scores=inputScores.toArray(new Double[inputScores.size()]);
Arrays.sort(scores);
min=scores[0];
max=scores[scores.length-1];
for(double score :scores){
total += score;
}
avg=total/scores.length;
System.out.println("總成績是" + total);
System.out.println("最高分是" + max);
System.out.println("最低分是" + min);
System.out.println("平均分是" + avg);
}
}
請問JAVA中如何編程求3個學生的三科成績的平均分?
平均分和總和都求了
public class Main {
public static void main(String[] args) {
int[] a = new int[] { 60, 70, 80 };
System.out.println("總分是:" + getSum(a));
System.out.println("平均分是:" + getAvg(a));
}
// 獲得總分
public static int getSum(int[] a) {
int sum = 0;
for (int i = 0; i a.length; i++) {
sum += a[i];
}
return sum;
}
// 獲得平均分
public static int getAvg(int[] a) {
int sum = 0;
for (int i = 0; i a.length; i++) {
sum += a[i];
}
return sum / a.length;
}
}
運行結果:
總分是:210
平均分是:70
java編程,三十名同學數(shù)學成績輸入一堆數(shù)組中,求平均分,求總分,求最高分和最低分
非常推薦用Java8的新特性Stream來解決這類求數(shù)據(jù)統(tǒng)計結果的,真的很方便,代碼簡潔而優(yōu)雅
用到了IntSummaryStatistics類,這個類就包含了題主說的各種統(tǒng)計結果了
ListStudent?list?=?Arrays.asList(new?Student(100),?new?Student(59),?new?Student(80),?new?Student(92));
IntSummaryStatistics?summaryStatistics?=?list.stream().mapToInt(Student::getScore).summaryStatistics();
System.out.println("最高分:"?+?summaryStatistics.getMax());
System.out.println("最低分:"?+?summaryStatistics.getMin());
System.out.println("總分:"?+?summaryStatistics.getSum());
System.out.println("平均分:"?+?summaryStatistics.getAverage());
可以參考了解一下
當前文章:java平均分總分代碼 java均分算法
本文URL:http://www.ef60e0e.cn/article/doedesg.html