赞
踩
案列演示,统计十个学生的平均分 public static void main(String[] args)throws Exception { double[] scores=new double[11];//长度为11 double sum=0,average=0; Scanner sc=new Scanner(System.in); //输入部分 for (int i=1;i<scores.length;i++){ System.out.print("scores["+i+"] = "); scores[i]=sc.nextDouble(); } //输出部分 for (int i=1;i<scores.length;i++){ sum+=scores[i]; } average=sum/scores.length; System.out.println(scores.length-1+"个学生的总平均成绩为:"+String.format("%.2f",average)); } } 采用while循环求10个学生成绩平均分 double[] scores = new double[11]; double sum = 0, average; Scanner sc = new Scanner(System.in); int i = 1; while (i<scores.length) { System.out.print("scores[" + i + "]="); scores[i] = sc.nextDouble(); sum = scores[i] + sum; i++; } average = sum / scores.length - 1; System.out.println(scores.length - 1 + "个学生的平均成绩为:" + String.format("%.2f", average));
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。