两个小例子

1、输入某年某月某日,判断这一天是这一年的第几天

Scanner scanner = new Scanner(System.in);
System.out.println("请输入日期,格式是yyyy-MM-dd");
String data = scanner.next();
String[] datas = data.split("-");
int year = Integer.valueOf(datas[0]);
int month = Integer.valueOf(datas[1]);
int day = Integer.valueOf(datas[2]);
int sum = 0;
for (int i = 1;i

2.录入本班学生姓名,成绩,自动计算出平均分,并且得出第一名。

Scanner scanner = new Scanner(System.in);
Map str = null;
List> stuliList = new ArrayList>();
//输入多个数字,求其总和
for(int i = 1;i<5;i++){
    System.out.println("请输入第"+i+"个姓名:");
    String name = scanner.next();
    System.out.println("请输入第"+i+"个成绩:");
    String score = scanner.next();
    str = new HashMap();
    //HashMap键值对的方式使用put添加元素
    str.put("name", name);
    str.put("score", score);
    stuliList.add(str);
}
System.out.println(stuliList.size());

Double sum = 0D;
Double avg = 0D;
Double max = 0D;

for (Map salmap : stuliList) {

    //获取集合中的薪资
    String score = salmap.get("score");
    Double sumscore = Double.parseDouble(score);
    System.out.println("成绩:" + score);
    //计算和
    sum += sumscore;
    System.out.println("SUM:" + sum);
    //计算平均值
    avg=sum/stuliList.size();
    System.out.println("avg:" + avg);

    for(int i = 0;imax){
            max=sumscore;
        }*/
        max = Math.max(max,sumscore);
    }

    System.out.println("最大值:" + max);

}

你可能感兴趣的:(两个小例子)