import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; public class RegisterApp { /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { // TODO Auto-generated method stub //(a)使用命令行参数,输入学生名单,和 班级名称 // 使用格式: java RegisterApp list.txt wl121 if(args.length != 2){ System.out.println("参数输入不对"); System.out.println("使用方法(示例):java RegisterApp 名单文件名称 班级名称"); System.exit(0); } //(b)学生签到结果:学生到,输入1;缺课,输入0 System.out.println("——————————————————"); System.out.println("简易学生签到程序V0.1"); System.out.println("老师叫到名字,请答‘到’"); System.out.println("1:到课 0:缺课"); System.out.println("——————————————————"); //(c)取得系统当前日期时间 Date now = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHMM");//可以方便地修改日期格式 String strDate = dateFormat.format( now ); System.out.println("当前时间:"+strDate); //(d)读取学生名单,args[0]为学生名单文件,args[1]为班级名称 String fileList = args[0]; String fileCheck = args[1] + strDate + ".txt"; File fileInput = new File(fileList); File fileOutput = new File(fileCheck); //(e)利用Scanner类读取文本数据/键盘输入数据; PrintWriter类把签到结果写入到文件 Scanner input = new Scanner(fileInput); Scanner sc = new Scanner(System.in); PrintWriter output = new PrintWriter(fileOutput); //保存缺课学生名字strAbsent ,缺课学生人数nAbsent, 是否缺课标记flag String strAbsent = ""; int nAbsent = 0; int flag = 0; while(input.hasNext()){ //循环读取学生数据 String strName = input.nextLine(); if(strName.equals("27 魏凯毅")) { continue; } //修改代码 //if(input.nextLine()=="")continue; //把学生名字输出到屏幕,从而进行点名。 //老师根据学生到课情况,输入1-到课,0-缺课,保存到flag中 System.out.println(strName); flag = sc.nextInt(); //如果缺课,则记录下缺课学生数目 与 名字 if(flag==0){ nAbsent = nAbsent+1; strAbsent = strAbsent + " " + strName; } //把考勤结果写入名单 output.print(strName); output.print(" "); output.println(flag); } //关闭I/O管道 sc.close(); output.close(); input.close(); System.out.println("——————————————————————————"); System.out.println("考勤结束."); System.out.printf("一共有%d个同学缺课,分别是:%s\n",nAbsent,strAbsent); System.out.println("——————————————————————————"); } }
修改点名源代码
最新推荐文章于 2024-01-19 16:26:28 发布