JAVA -从键盘上输入若干个学生的成绩,统计并输出最高成绩、最低成绩和平均成绩,当输入负数时结束输入。

        1. package com.hg.demo;
        2. import java.util.Scanner;
        3. public class demo02 {
        4.   public static void main(String[] args) {
        5.          // TODO Auto-generated method stub
        6.         double max = 0;
        7.         double min = 0;
        8.         double sum = 0;
        9.         
        10.          Scanner scanner = new Scanner(System.in);
        11.          System.out.println("请输入学生成绩:");
        12.          double b= scanner.nextDouble();
        13.          min = b;
        14.          max = b;
        15.          sum+=b;
        16.          int m = 1;
        17.          while(scanner.hasNextDouble()) {
        18.                 double a = scanner.nextDouble();
        19.                 if (a<0) {
        20.                        break;
        21.                 }
        22.                 if (a>max) {
        23.                        max = a;
        24.                 }
        25.                 if (a
        26.                        min = a;
        27.                 }
        28.                 sum += a;
        29.                 m++;
        30.          }
        31.          System.out.println("最大值为:"+max);
        32.          System.out.println("最小值为:"+min);
        33.          System.out.println("总和为:"+sum);
        34.          System.out.println("个数为:"+m);
        35.          System.out.println("平均值为:"+sum/m);
        36.         
        37.          scanner.close();
        38.   }
        39.  
        40. }

你可能感兴趣的:(java,开发语言)