package com.username;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Scanner;
public class CallUserName {
/**
* @param args
* 程序刚运行时产生一个随机姓名
接着提示用户是否继续?如果输入0就停止,否则继续
考点 while循环 随机数 数组
* @throws IOException
* @throws InterruptedException
*/
public static void main(String[] args) throws IOException, InterruptedException {
//创建文本输入流
BufferedReader br = new BufferedReader(new FileReader("E:\\userName\\a.txt"));
//创建集合存储元素
ArrayList<Integer> list = new ArrayList<Integer>();
//创建双列集合并关联键与映射
HashMap<Integer,String> hm = new HashMap<Integer, String>();
//定义映射键
Integer indexs = 0;
//键盘获取
Scanner sc = new Scanner(System.in);
String str = "";
while (true) {
//逐行读取字符串
String s = br.readLine();
//System.out.println("我执行了");
if(s == null) {
break; //如果达到文件末尾就退出循环
}else {
str += s;
}
}
//关闭输入流
br.close();
//System.out.println(str);
//切割字符串
String[] str1 = str.split(";");
for (int i = 0; i < str1.length; i++) {
//将截取字符串和维护键添加入双列集合
hm.put(indexs, str1[i]);
//将维护键添加入list集合
list.add(indexs);
//键自增
indexs++;
}
//System.out.println(list);
//System.out.println(hm.get(0));
for (;;) {
if (list.size() < 7) {
break;
}
//使用默认随机源对指定列表进行置换
//Collections.shuffle(list);
int[] in = {-1,-1,-1,-1,-1,-1,-1};
int count = 0;
for (; ;) {
int key = (int)(Math.random() * (list.size() -1));
//System.out.println(key);
for (int j = 0; j < in.length; j++) {
if(key == in[j]) {
key = -1;
break;
}
}
if (key != -1) {
in[count++] = key;
}
if(count == in.length ) {
break;
}
}
for (int i = 0; i < in.length ; i++) {
Thread.sleep(1000);
if (i == 6) {
System.out.println(hm.get(in[i]) + "被点名!");
}else {
System.out.println(hm.get(in[i]));
}
}
System.out.println("s键退出,任意键继续!");
//String s = sc.nextLine();
if (sc.nextLine().equals("s" )) {
break;
}
}
}
} |
|