随机点名的java实现(版本三)

此版本在二的基本上添加了二次随机功能,使得学生被点的概率比较公平。、

基本思路是:在产生随机下标时,先进行第一次随机,得到一个随机下标,再进行第二次随机,第二次随机的结果只能是0和1;若第次随机的结果为1,则最终的结果以第一次随机的结果为准;若第二次随机的结果为0,并且第一次随机点到的学生的被点次数小于平均点名次数,则最终的结果以第一次随机的结果为准,若大于平均点名次数,则进行递归。

具体实现代码如下:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class DianMing {
	public static void main(String[] args) throws Exception {
		ArrayList<Student> nameList = getStudentList();
		int avg = getAvg(nameList);
		int i = getStudent(nameList, avg);
		Student s = nameList.get(i);
		System.out.println(s.getName());
		nameList.get(i).setTime(s.getTime() + 1);
		// 若输入的内容为t,则天示回答正确定,若输入的为f则表示回符错误
		Scanner cin = new Scanner(System.in);
		System.out.println("是否回答正确(是为t,否为f):");
		String answer = cin.next();
		if ("t".equals(answer))
			nameList.get(i).setRight(s.getRight() + 1);
		File f = new File("src/name.txt");
		f.delete();
		writeStudents(nameList, f);
	}
	/**
	 * 把学生信息写回到txt文本文件中
	 * @param nameList
	 * @param f
	 * @throws FileNotFoundException
	 */
	private static void writeStudents(ArrayList<Student> nameList, File f)
			throws FileNotFoundException {
		PrintStream ps = new PrintStream(new FileOutputStream(f, true));
		for (Student st : nameList)
			ps.println(st.getName() + "\t" + st.getTime() + "\t"
					+ st.getRight());
		ps.close();
	}

	/**
	 * 是否直接点名,若随机值为1则直接点名, 否则的话判断被点名次数是否大于平均点名次数, 若是则再次随机,否则被点中
	 */
	public static int getStudent(ArrayList<Student> studentList, int avg) {
		Student s = null;
		Random r = new Random();
		int i = r.nextInt(studentList.size());
		s = studentList.get(i);
		int flag = r.nextInt(2);
		if (flag == 1)
			return i;
		else {
			if (avg > s.getTime())
				return i;
			else
				return getStudent(studentList, avg);
		}

	}

	// 求平均值
	public static int getAvg(ArrayList<Student> studentList) {
		int avg = 0;
		int total = 0;
		for (Student s : studentList)
			total += s.getTime();
		if (studentList.size() != 0)
			avg = total / studentList.size();
		return avg;
	}

	// 从文本文件中获取学生信息
	public static ArrayList<Student> getStudentList() throws Exception {
		Scanner cin = new Scanner(new FileInputStream(new File("src/name.txt")));
		ArrayList<Student> nameList = new ArrayList<Student>();
		cin.useDelimiter("\n");
		while (cin.hasNext()) {
			String s = cin.next();
			Student student = new Student();
			String[] sArr = s.split("\\s");
			student.setName(sArr[0]);
			student.setTime(Integer.parseInt(sArr[1]));
			nameList.add(student);
		}
		cin.close();
		return nameList;
	}

	// 把修改过的内容重新写回txt文件本中
	public static void rewriterStudentInfo(ArrayList<Student> nameList)
			throws Exception {
		File f = new File("src/name.txt");
		f.delete();
		if (!f.exists())
			f.createNewFile();
		PrintStream ps = new PrintStream(new FileOutputStream(f));
		for (Student st : nameList)
			ps.println(st.getName() + "\t" + st.getTime());
		ps.close();
	}
}

 

转载于:https://my.oschina.net/mingdegewu/blog/120967

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值