//循环录入某学生 5 门课的成绩并计算平均分, //如果某分数录入为负,停止录入并提示录 //入错误 public static void test6(){ Scanner sc = new Scanner(System.in); int socre = 0; //平均分 int sum = 0; //计算和 for(int i = 1;i<=5;i++){ System.out.println("请输入第"+i+"门课程的成绩:"); socre = sc.nextInt(); if(socre<=0){ System.out.println("输入错误,不能输入负数"); break; //结束循环 } sum+=socre; //将每次的成绩累积加到sum里面 } int avg = sum/5; System.out.println("平均分是:"+avg); }

相关文章:

  • 2022-12-23
  • 2021-06-04
  • 2022-12-23
  • 2021-07-15
  • 2022-12-23
  • 2022-12-23
  • 2021-07-11
猜你喜欢
  • 2021-09-23
  • 2022-12-23
  • 2021-12-18
  • 2022-12-23
  • 2021-06-13
  • 2022-12-23
  • 2022-01-21
相关资源
相似解决方案