输入一个同学各科的高考成绩,请计算出他的总成绩和平均成绩,共4个科目

1、有效得分范围为0-150,无效得分不在总分计算范围内(但科目永远是4) 2、单科成绩均为整数,平均成绩只取整数部分 输入: 字符串(各科目成绩间用空格分割) 输出: 总成绩+空格+平均成绩,如320 80 样例输入: 80 90 80 90 样例输出: 340 85

参考解答

public static void main(String[] args) {
  System.out.println(summary("80 90 80 90"));
  System.out.println(summary("80 90 80 -1"));
}

public static String summary(String input) {
  String[] scores = input.split(" ");
  int sum = 0;
  for (int i = 0; i < scores.length; i++) {
    int score = Integer.parseInt(scores[i]);
    if (score < 0 || score > 150) {
      continue;
    }
    sum += score;
  }
  return sum + " " + (sum / 4);
}

results matching ""

    No results matching ""