java,从键盘输入5个数,求出这5个数平均值(循环语句)

2024-11-26 12:39:11
推荐回答(3个)
回答1:

假设这几个数是array={3,8,3,7,6}
int s = 0;
for(int i=0;i<5;i++) {
s = s + array[i]
}
平均数=s/5;

回答2:

Scanner sc=new Scanner(System.in);
double sum=0;
while(true)
{
sum=0;
for(int i=0;i<5;i++)
{
Double d=sc.nextDouble();
sum+=d;
}
System.out.println(sum/5);
}

回答3:

看代码:
import java.util.Scanner;

public class AverageInput {
private static Scanner scan = new Scanner(System.in);
public static void main(String args[]){
System.out.println("请输入五个数字");
int[] target = new int[5];
int mini = 0;
int sum = 0;
for(int i=0; i<5; i++){
target[i] = scan.nextInt();
sum += target[i];
if (i==0 || mini > target[i]){
mini = target[i];
}
}
System.out.println("最小的数字是: " + mini);
System.out.println("平均值是: " + sum/5);
}
}