资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java平均分总分代码 java均分算法

JAVA代码问题!输入5名学生的成绩,并计算总成绩,平均分,最高分,最低分

public static void main(String[] args) {

成都创新互联公司作为成都网站建设公司,专注成都网站建设公司、网站设计,有关成都定制网站方案、改版、费用等问题,行业涉及砂岩浮雕等多个领域,已为上千家企业服务,得到了客户的尊重与认可。

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)+"个分数:");

}

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("您输入的分数不是数值类型,");

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("请输入要统计学生的成绩(理论上可以输入无限个,前提是你有那么大的内存):");

while(true){

try{

System.out.println("请输入第"+count+++"个分数,或输入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("您输入的分数不是数值类型,");

System.out.println("若想结束请输入exit ,若想计算结果请输入ok");

count--;

}

}

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

System.out.println("您没有输入学生成绩,无数据可统计,程序结束。");

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编程,三十名同学数学成绩输入一堆数组中,求平均分,求总分,求最高分和最低分

非常推荐用Java8的新特性Stream来解决这类求数据统计结果的,真的很方便,代码简洁而优雅

用到了IntSummaryStatistics类,这个类就包含了题主说的各种统计结果了

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均分算法
转载源于:http://cdkjz.cn/article/doedesg.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220