题目描述
本题要求编写程序,计算4个整数的和与平均值。题目保证输入与输出均在整型范围内。
输入格式:
输入在一行中给出4个整数,其间以空格分隔。
输出格式:
在一行中按照格式“Sum = 和; Average = 平均值”顺序输出和与平均值,其中平均值精确到小数点后一位。
public class expect {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a,b,c,d;
a = sc.nextInt();
b = sc.nextInt();
c = sc.nextInt();
d = sc.nextInt();
int sum = 0;
sum = a+b+c+d;
float ave = (float) (sum / 4.0);
System.out.println("sum = "+sum);
System.out.println("ave = "+ave);
}
}
最后得到运行结果:
是自己写代码的第二天。