public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int number;
int count = 0;
double num = 0;
System.out.println("请输入值:");
//while的语法方式
// number = in.nextInt();
// while (number >0) {
// num = num + number;
// count = count + 1;
// number = in.nextInt();
// }
//do-while的语法方式
do {
number = in.nextInt();
if (number > 0) {
num = num + number;
count = count + 1;
}
} while (number > 0);
if (count > 0) {
System.out.println("输出平均数:" + (num / count));
}
}
注:
有俩种方式供选择!