嗨705 2022-12-09 09:57 采纳率: 60%
浏览 41
已结题

输入学生成绩,在0到100以内计算平均数否则就停止计算。

输入学生成绩,在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("未输入有效成绩");
            }
        }
    }
    
    
    本回答被专家选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 12月21日
  • 专家已采纳回答 12月13日
  • 创建了问题 12月9日