利用三个包装类编写一个计算三个整数平均值的程序。程序首先提示用户输入3个整数,然后将3个整数分别赋予3个Integer对象。利用适当的包装类方法将3个整数转换成整数值。将其平均值包装到一个Double对象。最后打印这三个整数值以及平均值。程序将继续运行直到用户打入“n”。
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Integer in[] = new Integer[3];
int count = 0;
boolean isValid = false;
while(!isValid){
if (sc.hasNextInt()) {
String str = sc.next();
in[count++] = Integer.parseInt(str);
if (count == 3) {
Double dou = new Double((in[0].intValue() + in[1].intValue() + in[2].intValue()) / 3.0);
System.out.print(in[0].intValue() + "\t" + in[1].intValue() + "\t" + in[2].intValue());
System.out.println("\t" + dou.doubleValue());
sc.nextLine();
count = 0;
}
} else if (sc.next().equals("n")) {
isValid = true;
} else {
System.out.println("Please input an integer or \"N\".");
sc.nextLine();
}
}
}
}