输入学生成绩,在0到100以内计算平均数否则就停止计算,是用for和if吗
5条回答 默认 最新
道爷我成啦 2022-12-09 10:17关注如有帮助,望采纳
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("未输入有效成绩"); } } }本回答被专家选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报