Java控制台在线考试系统
需求
考试系统
1)读取items.txt文件
2)将所有的试题存入到集合中(按照对象存储)
3)将集合中所有的试题 遍历展示(只展示 标题、选项)
4)开始考试 用户通过Scanner录入指定的字符串,完成如下功能
查看上一题 下一题 选择答案 记录每个题对应答案
查看得分
将本次考试成绩保存到文件
查看上次对应的成绩
1.文件读取到内存中
BufferedReader br=;
List temp=new ArrayList<>();
2.内存中存储所有的试题 List Set Map 集合
List list=new ArrayList<>();
Set<Item> set=new LinkedHashSet<>();
Map<Iteger,Item> map=new HashMap<>();
3.遍历集合,每次输出一道题
prev
next
currentIndex
4.录入用户选项
Scanner scanner=new Scanner(System.in);
记录自己的答案
List list.set(index,String);
数组 array[1]=String;
map map.put(index,String);
5.交卷 比对答案 给出当次成绩
正确答案
自选答案
6.查看历史成绩 成绩文件(追加)
成绩存储到score.txt
试题Items.txt
第1题 Java语言是哪年发明的?
A. 1991
B. 1994
C. 1996
D. 1999
A
第2题 下列关于JDK、JRE和JVM的描述,哪项正确?
A. JDK中包含了JRE,JVM中包含了JRE
B. JDK中包含了JRE,JRE中包含了JVM
C. JRE中包含了JDK,JVM中包含了JRE
D. JRE中包含了JDK,JDK中包含了JVM
B
第3题 用于生成Java文档的JDK工具是?
A. javac
B. jdb
C. javadoc
D. junit
C
第4题 使用JDK工具生成的Java文档的文件格式是?
A. XML格式
B. HTML格式
C. 二进制格式
D. 自定义格式
B
第5题 以下关于支持Java运行平台的叙述,哪项错误?
A. Java可在Solaris平台上运行
B. Java可在Windows平台上运行
C. Java语言与平台无关。Java程序的运行结果与操作系统无关
D. Java语言与平台无关。Java程序的运行结果依赖于操作系统
D
第6题 JVM在执行一个Java类时,大致采用以下过程?
A. 装载类->校验类->执行类中的代码
B. 校验类->装载类->执行类中的代码
C. 装载类->执行类中的代码->校验类
D. 执行类中的代码->装载类->校验类
A
第7题 用于编译Java源文件的JDK工具是?
A. javac
B. jdb
C. java
D. junit
A
第8题 Java程序的跨平台特征是由以下哪项技术实现的?
A. 系统硬件
B. JVM
C. Java编译器
D. 操作系统
B
第9题 下列有关类、对象和实例的叙述,正确的是哪一项?
A. 类就是对象,对象就是类,实例是对象的另一个名称,三者没有差别
B. 类是对象的抽象,对象是类的具体化,实例是对象的另一个名称
C. 对象是类的抽象,类是对象的具体化,实例是对象的另一个名称
D. 类是对象的抽象,对象是类的具体化,实例是类的另一个名称
B
第10题 Java源文件的后缀名是?
A. .class
B. .c
C. .java
D. .txt
C
Item
package com.lanou;
public class Item {
private String title;
private String optionA;
private String optionB;
private String optionC;
private String optionD;
private String answer;
public Item() {
super();
}
public Item(String title, String optionA, String optionB, String optionC, String optionD, String answer) {
super();
this.title = title;
this.optionA = optionA;
this.optionB = optionB;
this.optionC = optionC;
this.optionD = optionD;
this.answer = answer;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getOptionA() {
return optionA;
}
public void setOptionA(String optionA) {
this.optionA = optionA;
}
public String getOptionB() {
return optionB;
}
public void setOptionB(String optionB) {
this.optionB = optionB;
}
public String getOptionC() {
return optionC;
}
public void setOptionC(String optionC) {
this.optionC = optionC;
}
public String getOptionD() {
return optionD;
}
public void setOptionD(String optionD) {
this.optionD = optionD;
}
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
@Override
public String toString() {
return "Item [title=" + title + ", optionA=" + optionA + ", optionB=" + optionB + ", optionC=" + optionC
+ ", optionD=" + optionD + ", answer=" + answer + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((answer == null) ? 0 : answer.hashCode());
result = prime * result + ((optionA == null) ? 0 : optionA.hashCode());
result = prime * result + ((optionB == null) ? 0 : optionB.hashCode());
result = prime * result + ((optionC == null) ? 0 : optionC.hashCode());
result = prime * result + ((optionD == null) ? 0 : optionD.hashCode());
result = prime * result + ((title == null) ? 0 : title.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Item other = (Item) obj;
if (answer == null) {
if (other.answer != null)
return false;
} else if (!answer.equals(other.answer))
return false;
if (optionA == null) {
if (other.optionA != null)
return false;
} else if (!optionA.equals(other.optionA))
return false;
if (optionB == null) {
if (other.optionB != null)
return false;
} else if (!optionB.equals(other.optionB))
return false;
if (optionC == null) {
if (other.optionC != null)
return false;
} else if (!optionC.equals(other.optionC))
return false;
if (optionD == null) {
if (other.optionD != null)
return false;
} else if (!optionD.equals(other.optionD))
return false;
if (title == null) {
if (other.title != null)
return false;
} else if (!title.equals(other.title))
return false;
return true;
}
}
ExamManager
package com.lanou;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ExamManager {
//存储所有试题
static List<Item> allItem=new ArrayList<>();
//存储自己的答案
static String[] myAnswer=null;
//存储自己的成绩
static List<String> myScore=new ArrayList<>();
//临时集合
static List<String> tempList=new ArrayList<>();
static {
try {
initItem();
myAnswer=new String[allItem.size()];
// myScore=new String[allItem.size()];
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//初始化所有的试题
public static void initItem() throws IOException {
readFileToList();
for(int i=0;i<tempList.size();i+=7) {
String title=tempList.get(i);
String optionA=tempList.get(i+1);
String optionB=tempList.get(i+2);
String optionC=tempList.get(i+3);
String optionD=tempList.get(i+4);
String answer=tempList.get(i+5);
Item item=new Item(title,optionA,optionB,optionC,optionD,answer);
allItem.add(item);
}
System.out.println();
}
public static void readFileToList() throws IOException {
//读取文件内容封装到allItem
BufferedReader br=new BufferedReader(new FileReader("items.txt"));
//循环读取文件内容添加到集合
String str=null;
while((str=br.readLine())!=null) {
tempList.add(str);
}
br.close();
}
//开始考试
public static void startExam(int index) {
Item item=allItem.get(index);
System.out.println(item.getTitle());
System.out.println(item.getOptionA());
System.out.println(item.getOptionB());
System.out.println(item.getOptionC());
System.out.println(item.getOptionD());
}
public static void main(String[] args) throws IOException {
ExamManager manger=new ExamManager();
boolean flag=true;
int index=0;
Scanner scanner=new Scanner(System.in);
while(flag) {
startExam(index);
System.out.println("请输入你的答案:");
String answer=scanner.next();
myAnswer[index]=answer;
System.out.println("P 上一题,N 下一题,E 交卷");
String option=scanner.next();
switch(option) {
case "P":
index=index==0 ? index : --index;
break;
case "N":
index=index==allItem.size()-1 ? index : ++index;
break;
case "E":
flag=false;
break;
default:
System.out.println("录入错误!");
}
}
double score=judge();
System.out.println("score:"+score);
saveScoreToFile(score);
selectPrevScore();
System.out.println("退出系统。。。。。。");
}
public static double judge() {
double score=0;
//比对两个集合 allItem myAnswer
for(int i=0;i<allItem.size();i++) {
Item item=allItem.get(i);
if(item.getAnswer().equalsIgnoreCase(myAnswer[i])) {
score+=10;
}
}
return score;
}
public static void saveScoreToFile(Double score) throws IOException {
Writer writer=new FileWriter("score.txt",true);
BufferedWriter bw=new BufferedWriter(writer);
String s=score.toString();
bw.write(s);
bw.newLine();
bw.flush();
bw.close();
writer.close();
}
public static void selectPrevScore() throws IOException {
Reader reader=new FileReader("score.txt");
BufferedReader br=new BufferedReader(reader);
String score=null;
while((score=br.readLine())!=null) {
myScore.add(score);
// System.out.println(score);
}
br.close();
reader.close();
System.out.println("是否查看上次成绩?(Y/N)");
Scanner scanner=new Scanner(System.in);
String choose=scanner.next();
switch(choose) {
case "Y":
if(myScore.size()>1) {
String prevScore=myScore.get(myScore.size()-2);
System.out.println("上次对应的成绩:"+prevScore);
}else {
System.out.println("你未参加过考试!");
}
break;
case "N":
break;
default:
System.out.println("输入错误!");
}
}
}