输入学生成绩,在0到100以内计算平均数否则就停止计算,是用for和if吗
如有帮助,望采纳
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int scoreSum = 0;
int index = 0;
while (true) {
int score = scanner.nextInt();
if (score >=0 && score <= 100) {
scoreSum += score;
index++;
} else {
break;
}
}
if (index != 0) {
System.out.println("平均分为:" + (double)scoreSum/index);
} else {
System.out.println("未输入有效成绩");
}
}
}
这个要用到while循环来作为 继续计算的判别
然后使用 if判别是否停止,停止就break,结束循环,应该用不到for
能举个栗子吗,成绩相加不就超100了吗
yes
for while 各种循环都行,无非就是一个满足条件break跳出循环,0-100的区间肯定是要if判断的