输入任意多个数字,以-1结束输入;计算它们的平均值并输出。

import java.util.Scanner;

public class Avrage {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    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);
}

}
最后修改:2019 年 12 月 31 日 11 : 05 PM
如果觉得我的文章对你有用,请随意赞赏