修改并运行“简单签到程序”。
增加功能:让程序过滤掉自己,不被点名。
结果:把修改后的程序发布到自己的博客空间。博文包括 源代码 以及 运行结果。
首先根据老师给的源码和要求,我们可以简单的给出修改方案 就是讲其中一段修改成这样
/**************************************************************************************************/
if(strName.equals("5 陈鹏"))
{
flag=1;
}
else{
System.out.println(strName);
flag = sc.nextInt();
}
/**************************************************************************************************/
然后,全部代码如下
/**
* 程序功能:简单的签到程序,能够保存签到后的结果
* 原作者:丁又专
* 修改:陈鹏
* 修改版本:00001
*
*/
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();
//把学生名字输出到屏幕,从而进行点名。
//老师根据学生到课情况,输入1-到课,0-缺课,保存到flag中
/*******************************************修改部分*******************************************************/
if(strName.equals("5 陈鹏"))
{
flag=1;
}
else{
System.out.println(strName);
flag = sc.nextInt();
}
/**************************************************************************************************/
//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("——————————————————————————");
}
}
从上面可以看出,在第七十行做出了修改,但是在实验室里老师说这个太过于简单,虽然满足基本需求,但是程序做死了不太好,所以,就按照老师新的一个要求,这个版本,需要一个跳过名单passname.txt文件。文件只需要按行保存所需要跳过的名单就可以。
代码如下
/**
* 程序功能:简单的签到程序,能够保存签到后的结果
* 源作者:丁又专
* 修改:陈鹏
* 修改次数:00002
*
*/
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);
/*********************************************************************************/ //获取跳过名单,但是目前只能有一个
File passnamefile = new File ("passname.txt");
Scanner passin = new Scanner(passnamefile);
String passname = passin.nextLine();
/*********************************************************************************/
//(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();
//把学生名字输出到屏幕,从而进行点名。
//老师根据学生到课情况,输入1-到课,0-缺课,保存到flag中
/**************************************************************************************************/
if(strName.equals(passname))
{
flag=1;
}
else{
System.out.println(strName);
flag = sc.nextInt();
}
/**************************************************************************************************/
//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("——————————————————————————");
}
}
但是,这个版本出来后,可以发现只能有一个人可以跳过,这样也就有一个问题,是否可以多个人跳过?
未完待续