日期:2014-05-20 浏览次数:20743 次
package cc.openhome;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Average3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double sum = 0;
int count = 0;
int number;
while (true) {
try {
number = scanner.nextInt();
if (number == 0) {
break;
}
sum += number;
count++;
} catch (InputMismatchException e) {
System.out.printf("略过非整数输入: %s%n", scanner.next());
} //为什么这里使用.nextInt()会出错?
}
System.out.printf("平均 %.2f%n", sum / count);
}
}