假设这几个数是array={3,8,3,7,6}
int s = 0;
for(int i=0;i<5;i++) {
s = s + array[i]
}
平均数=s/5;
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);
}
看代码:
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);
}
}