丁老師的作業_修改並運行“簡單簽到程序”

修改並運行“簡單簽到程序”。 
增加功能:讓程序過濾掉自己,不被點名。 
學生名單下載:http://pan.baidu.com/s/1sjCpxMX 
“簡單簽到程序”網頁:http://blog.csdn.net/dyz1982/article/details/20311823  
結果:把修改後的程序發佈到自己的博客空間。博文包括 源代碼 以及 運行結果。 

首先根據老師給的源碼和要求,我們可以簡單的給出修改方案   就是講其中一段修改成這樣
			/**************************************************************************************************/
			
			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("——————————————————————————");

	}

}

但是,這個版本出來後,可以發現只能有一個人可以跳過,這樣也就有一個問題,是否可以多個人跳過?


未完待續

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章