//求平均成绩,且无固定人数,
import java.util.Scanner;
public class ScoreAverage{
public static void main(String[]args){
Scanner scanner=new Scanner(System.in);
int score=0;
int sum=0;
int count=-1;
while(score!=-1){
count++;
sum=sum+score;
System.out.print("请输入分数(-1)结束: ");
score=scanner.nextInt();
}
System.out.print("平均:"+sum/count);
}
}
//求平均成绩,且有固定人数,
import java.util.Scanner;
public class AverageInput{
public static void main(String[]args){
Scanner scanner=new Scanner(System.in);
System.out.print("请输入学生人数:");
int length=scanner.nextInt();//将字符转成整数
float[]score=new float [length];//动态的配置长度
//遍历成绩
for(int i=0;i<score.length;i++){
System.out.print("请输入分数 :");
float input=scanner.nextFloat();
score[i]=input;
}
System.out.print(""n分数");
float total=0;
for(int i=0;i<score.length;i++){
total=total+score[i];
System.out.print(score[i]+" ");
}
System.out.printf(""n平均:%.2f",total/score.length);//.2f精度为二位
}
}
posted on 2009-03-23 20:58
鹏凌 阅读(122)
评论(0) 编辑 收藏