Java---点名---最简

晚上有课,不想去,但是又怕老师点名。上课之前为了避免陷入LOL,那就先靠java打法一下时间。

一个简单的Java 语音 点名程序:(jdk-1.6下完成的)

  • 先用一个文件来存储学生的信息:因为短时间做,先用txt文件为例,数据库是比较好的选择吧。
  • Java将数据读取并存储到Vector数组中。
  • 下面就可以对学生进行点名了。
  • 可以再加一些对学生点名时会遇到的一些问题进行处理。
  • 先放下结果文件:在这里插入图片描述


  1. 学生的信息文本:Student.txt(姓名,学号,性别,是否请假)
    文轩0	201817020000	男	0
    文轩1	201817020001	男	1
    文轩2	201817020002	女	1
    文轩3	201817020003	男	1
    文轩4	201817020004	男	1
    文轩5	201817020005	男	1
    文轩6	201817020006	女	1
    文轩7	201817020007	女	0
    文轩8	201817020008	男	1
    
  2. 读取学生信息的类:StudentList.java
    package 点名;
    
    /**
     * 主类
     */
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.util.Vector;
    
    public class StudentList {
    	static final int N = 200;
    	static Vector<Student> arr = new Vector<Student>();
    
    	public static void add() throws Exception {// 读人头
    		BufferedReader bReader = new BufferedReader(new FileReader("D:\\Java\\签到\\点名\\src\\点名\\StudentHome.txt"));
    		String str = null;
    		while ((str = bReader.readLine()) != null) {
    			String[] str_list = str.split("\t");
    			arr.add(new Student(str_list[0], str_list[1], str_list[2], (str_list[3].equals("0") ? false : true)));
    		}
    		bReader.close();
    	}
    
    	public static void main(String[] args) throws Exception {
    		add();
    		new CallTheRoll(arr);// 开始
    	}
    }
    
    
  3. 开始点名,并进行简单的处理:CallTheRoll.java
    package 点名;
    
    import java.util.Scanner;
    import java.util.Vector;
    
    public class CallTheRoll {
    	static Scanner sc = new Scanner(System.in);
    
    	public CallTheRoll(Vector<Student> a) throws Exception {
    		System.out.println("是否开始点名?");
    		System.out.println("是\t否");
    		System.out.print("指令:");
    		if (sc.next().equals("是")) {
    			for (Student m : a) {
    				dian(m);
    			}
    		} else {
    			System.out.println("退出点名");
    		}
    		sc.close();
    		System.out.println("签到结束");
    	}
    
    	public void dian(Student m) throws Exception {
    		new TestToSpeech(m.name).read();
    		System.out.print(m.name + "到了吗?: ");
    		if (m.state == false) {
    			System.out.println(m.name + "已请假" + "\n");
    			return;
    		}
    		m.state = (sc.nextInt() == 1) ? true : false;
    		System.out.println(m.id + " " + m.name + ":" + ((m.state) ? "已签到" : "签到失败") + "\n");
    	}
    }
    
  4. 存储学生信息的类:Student.java
    package 点名;
    
    public class Student {
    	String name;// 姓名
    	int age;// 年龄
    	String id;// 学号
    	String sex;// 性别
    	String professional;// 专业
    	String img_path;// 照片
    	boolean state;// 是否请假
    
    	public Student() {
    		// TODO 自动生成的构造函数存根
    	}
    
    	public Student(String name, String id, String sex, boolean state) {// 基本信息
    		super();
    		this.name = name;
    		this.id = id;
    		this.sex = sex;
    		this.state = state;
    	}
    
    	public Student(String name, int age, String id, String sex, String professional, String img_path) {// 全部信息
    		super();
    		this.name = name;
    		this.age = age;
    		this.id = id;
    		this.sex = sex;
    		this.professional = professional;
    		this.img_path = img_path;
    		this.state = true;
    	}
    
    	@Override
    	public String toString() {
    		return "Student [state=" + state + "]";
    	}
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public int getAge() {
    		return age;
    	}
    
    	public void setAge(int age) {
    		this.age = age;
    	}
    
    	public String getId() {
    		return id;
    	}
    
    	public void setId(String id) {
    		this.id = id;
    	}
    
    	public String getSex() {
    		return sex;
    	}
    
    	public void setSex(String sex) {
    		this.sex = sex;
    	}
    
    	public String getProfessional() {
    		return professional;
    	}
    
    	public void setProfessional(String professional) {
    		this.professional = professional;
    	}
    
    	public String getImg_path() {
    		return img_path;
    	}
    
    	public void setImg_path(String img_path) {
    		this.img_path = img_path;
    	}
    
    }
    
    
  5. 最关键的语音播放类:TestToSpeech.java
    package 点名;
    
    /**
     * 文本转语音
     * @author 轩xuan
     *
     */
    import com.jacob.activeX.ActiveXComponent;
    import com.jacob.com.Dispatch;
    import com.jacob.com.Variant;
    
    public class TestToSpeech {
    	String str = new String();
    
    	// 转入字符串
    	public TestToSpeech(String str){
    		this.str = str;
    //		read();
    	}
    
    	// 读
    	public void read() {
    		ActiveXComponent sap = new ActiveXComponent("Sapi.SpVoice");
    		Dispatch sapo = sap.getObject();
    		try {
    			// 音量 0-100
    			sap.setProperty("Volume", new Variant(100));
    			// 语音朗读速度 -10 到 +10
    			sap.setProperty("Rate", new Variant(0));
    			// 执行朗读
    
    			Dispatch.call(sapo, "Speak", new Variant(this.str));
    		} catch (Exception e) {
    			e.printStackTrace();
    		} finally {
    			sapo.safeRelease();
    			sap.safeRelease();
    		}
    	}
    
    //	public static void main(String[] args){
    //		new TestToSpeech("123");
    //	}
    }
    
    

用到的外部库:jacob.jar


《余泪泛澜兮繁花》---海涅
余泪泛澜兮繁花
余声悱亹兮莺歌
少女子兮
使君心其爱余
余将捧繁花而献之
流莺鸣其嘤嘤兮
傍吾欢之罘罳

该吃饭上课了,对LOL说:我太难了。

猜你喜欢

转载自blog.csdn.net/qq_44009311/article/details/103167850