Java读取文件随机提示并判断输入功能(背单词系统)的实现

这里演示了一个类似于背单词系统的Demo。

其详细代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

import java.io.File;
import java.util.Arrays;
import java.util.Scanner;

public class Test {
public static void main(String[] args) throws Exception {
Scanner sc=new Scanner(new File("E:\\remenber.csv"));
String firstLine=sc.nextLine();
int len=50;
String[] index=new String[len];
String[] description=new String[len];
int i=0;
while(sc.hasNextLine()&&i<len){
String[] temp=(sc.nextLine()).split(",");
index[i]=temp[0];
description[i]=temp[1];
i++;
}
Scanner input =new Scanner(System.in);
do {
int random=(int)(Math.random()*len);
if (description[random]==null) {
continue;
}else {
System.out.println("提示:"+description[random]);
}
String userInput=input.nextLine();

if ("exit".equals(userInput)) {
break;
}
if (userInput.equals(index[random])) {
System.out.println("You are right!");
}else{
System.out.println("输入错误!正确答案是:"+index[random]);
}
} while (true);
}
}
-------------本文结束感谢您的阅读-------------