输入任意多个数字,以-1结束输入;计算它们的平均值并输出。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| import java.util.Scanner;
public class Avrage {
public static void main(String[] args) { Scanner in = new Scanner(System.in); int number = 0,count = 0, temp; temp = in.nextInt(); while(temp != -1) { number = number + temp; count = count + 1; temp = in.nextInt(); } System.out.println("平均数为="+ 1.0*number/count); }
}
|