java平均分總分代碼 java均分算法

JAVA代碼問題!輸入5名學(xué)生的成績,并計算總成績,平均分,最高分,最低分

public static void main(String[] args) {

成都創(chuàng)新互聯(lián)公司作為成都網(wǎng)站建設(shè)公司,專注成都網(wǎng)站建設(shè)公司、網(wǎng)站設(shè)計,有關(guān)成都定制網(wǎng)站方案、改版、費用等問題,行業(yè)涉及砂巖浮雕等多個領(lǐng)域,已為上千家企業(yè)服務(wù),得到了客戶的尊重與認(rèn)可。

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名學(xué)生的成績:");

Scanner input = new Scanner(System.in);

while(count5){

try{

if(count 5){

System.out.println("請輸入第"+(count+1)+"個分?jǐn)?shù):");

}

inputStr=input.nextLine();

scores[count++]=Double.valueOf(inputStr.trim());

}catch(Exception e){

if(inputStr!=null "exit".equals(inputStr.trim())){

System.out.println("您已成功結(jié)束程序");

System.exit(0);

}

System.out.println("若想結(jié)束請輸入:exit");

System.out.print("您輸入的分?jǐn)?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)計學(xué)生的成績(理論上可以輸入無限個,前提是你有那么大的內(nèi)存):");

while(true){

try{

System.out.println("請輸入第"+count+++"個分?jǐn)?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("您已成功結(jié)束程序");

input.close();

System.exit(0);

}

if(inputStr!=null "ok".equals(inputStr.trim().toLowerCase())){

break;

}

System.out.println("您輸入的分?jǐn)?shù)不是數(shù)值類型,");

System.out.println("若想結(jié)束請輸入exit ,若想計算結(jié)果請輸入ok");

count--;

}

}

if(inputScores.size()==0){

System.out.println("您沒有輸入學(xué)生成績,無數(shù)據(jù)可統(tǒng)計,程序結(jié)束。");

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個學(xué)生的三科成績的平均分?

平均分和總和都求了

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;

}

}

運行結(jié)果:

總分是:210

平均分是:70

java編程,三十名同學(xué)數(shù)學(xué)成績輸入一堆數(shù)組中,求平均分,求總分,求最高分和最低分

非常推薦用Java8的新特性Stream來解決這類求數(shù)據(jù)統(tǒng)計結(jié)果的,真的很方便,代碼簡潔而優(yōu)雅

用到了IntSummaryStatistics類,這個類就包含了題主說的各種統(tǒng)計結(jié)果了

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());

可以參考了解一下

新聞標(biāo)題:java平均分總分代碼 java均分算法
本文鏈接:http://muchs.cn/article36/doedesg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、搜索引擎優(yōu)化、微信公眾號服務(wù)器托管、品牌網(wǎng)站制作、App開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計公司