这段代码是一个Java实体类,表示一个试题的模型。
该实体类具有以下属性:
id
:题号,表示试题的编号。title
:题目,表示试题的题目内容。optionA
:选项A,表示试题的选项A。optionB
:选项B,表示试题的选项B。optionC
:选项C,表示试题的选项C。optionD
:选项D,表示试题的选项D。answer
:答案,表示试题的正确答案。value
:分值,表示该试题的分值。
该实体类提供了对应属性的获取(get)和设置(set)方法,用于对试题的属性进行操作。
这个实体类用于表示试题的数据模型,可以在试题系统中使用,用来存储和获取试题的相关数据。
package demo1130;
import java.util.ArrayList;
import java.util.Scanner;
//(业务)功能类
public class QuestionTest {
public static void main(String[] args) {
new QuestionTest().start();
}
Scanner sc = new Scanner(System.in);
public void start() {
while (true) {
System.out.println("------------------------");
System.out.println("----请选择功能-----------");
System.out.println("----1-试题录入-----------");
System.out.println("----2-试题查询(按照题号查看)-----------");
System.out.println("----3-试题删除(按照题号删除)-----------");
System.out.println("----4-试题修改(按照题号修改)-----------");
System.out.println("----5-查看试题(查看全部试题)-----------");
System.out.println("----6-排序试题(根据题号排序)-----------");
System.out.println("----7-回答试题(正确数/得分)-----------");
System.out.println("----请输入功能序号:-----------");
switch (Integer.parseInt(sc.nextLine())) {
case 1:
inputs();
break;
case 2:
getModelById();
break;
case 3:
delModelById();
break;
case 4:
updModelById();
break;
case 5:
print();
break;
case 6:
sortById();
break;
case 7:
test();
break;
}
System.out.println("****是否退出系统? y/n ****");
if ("y".equalsIgnoreCase(sc.nextLine()))
break;
}
}
public void test() {//回答试题
int sum = 0;// 总分
int count = 0;// 正确数
for (QuestionModel model : list) {
System.out.println("题号:" + model.getId());
System.out.println("题目:" + model.getTitle());
System.out.println("选项A:" + model.getOptionA());
System.out.println("选项B:" + model.getOptionB());
System.out.println("选项C:" + model.getOptionC());
System.out.println("选项D:" + model.getOptionD());
// System.out.println("答案:"+model.getAnswer());
System.out.println("分值:" + model.getValue());
System.out.println("请输入答案:");
String line = sc.nextLine();
if (model.getAnswer().equalsIgnoreCase(line)) {
sum += model.getValue();
count++;
}
}
System.out.println("正确数" + count + "\t得分:" + sum);
}
public void sortById() {//排序
int len = list.size();
for (int i = 0; i < len - 1; i++) {
for (int j = 0; j < len - i - 1; j++) {
QuestionModel m1 = list.get(j);
QuestionModel m2 = list.get(j + 1);
if (m1.getId() > m2.getId()) {
list.set(j, m2);
list.set(j + 1, m1);
}
}
}
}
public void updModelById() {//修改
System.out.println("请输入即将修改的题号:");
int id = Integer.parseInt(sc.nextLine());
for (QuestionModel model : list) {
if (model.getId() == id) {
System.out.println("该题目信息如下:");
System.out.println("题号:" + model.getId());
System.out.println("题目:" + model.getTitle());
System.out.println("选项A:" + model.getOptionA());
System.out.println("选项B:" + model.getOptionB());
System.out.println("选项C:" + model.getOptionC());
System.out.println("选项D:" + model.getOptionD());
System.out.println("答案:" + model.getAnswer());
System.out.println("分值:" + model.getValue());
System.out.println("请进行修改操作:");
System.out.println("请输入题目:");
model.setTitle(sc.nextLine());
System.out.println("请输入选项A:");
model.setOptionA(sc.nextLine());
System.out.println("请输入选项B:");
model.setOptionB(sc.nextLine());
System.out.println("请输入选项C:");
model.setOptionC(sc.nextLine());
System.out.println("请输入选项D:");
model.setOptionD(sc.nextLine());
System.out.println("请输入答案:");
model.setAnswer(sc.nextLine());
System.out.println("请输入分值:");
model.setValue(Integer.parseInt(sc.nextLine()));
}
}
}
public void delModelById() {//删除
System.out.println("请输入即将删除的题号:");
int id = Integer.parseInt(sc.nextLine());
for (int i = list.size() - 1; i >= 0; i--) {
QuestionModel model = list.get(i);
if (model.getId() == id) {
list.remove(i);
System.out.println("该题号(" + ")已删除");
return;
}
}
System.out.println("该题号对应题目不存在");
}
public void getModelById() {//查询
System.out.println("请输入即将查询的题号:");
int id = Integer.parseInt(sc.nextLine());
for (QuestionModel model : list) {
if (model.getId() == id) {
System.out.println("题号:" + model.getId());
System.out.println("题目:" + model.getTitle());
System.out.println("选项A:" + model.getOptionA());
System.out.println("选项B:" + model.getOptionB());
System.out.println("选项C:" + model.getOptionC());
System.out.println("选项D:" + model.getOptionD());
System.out.println("答案:" + model.getAnswer());
System.out.println("分值:" + model.getValue());
return;
}
}
System.out.println("该题号对应题目不存在");
}
public void inputs() {//录入
while (true) {
input();
System.out.println("是否继续录入? y/n");
if ("n".equalsIgnoreCase(sc.nextLine()))
break;
}
}
public boolean isExist(int id) {// 判断题号是否存在
for (QuestionModel model : list) {
if (model.getId() == id) {
System.out.println("该题号(" + ")已经存在");
return true;
}
}
return false;
}
ArrayList<QuestionModel> list = new ArrayList<>();
public void input() {
QuestionModel model = new QuestionModel();
int id = 0;
do {
System.out.println("请输入题号:");
id = Integer.parseInt(sc.nextLine());
} while (isExist(id));
model.setId(id);
System.out.println("请输入题目:");
model.setTitle(sc.nextLine());
System.out.println("请输入选项A:");
model.setOptionA(sc.nextLine());
System.out.println("请输入选项B:");
model.setOptionB(sc.nextLine());
System.out.println("请输入选项C:");
model.setOptionC(sc.nextLine());
System.out.println("请输入选项D:");
model.setOptionD(sc.nextLine());
System.out.println("请输入答案:");
model.setAnswer(sc.nextLine());
System.out.println("请输入分值:");
model.setValue(Integer.parseInt(sc.nextLine()));
list.add(model);
}
public void print() {//查看
for (QuestionModel model : list) {
System.out.println("题号:" + model.getId());
System.out.println("题目:" + model.getTitle());
System.out.println("选项A:" + model.getOptionA());
System.out.println("选项B:" + model.getOptionB());
System.out.println("选项C:" + model.getOptionC());
System.out.println("选项D:" + model.getOptionD());
System.out.println("答案:" + model.getAnswer());
System.out.println("分值:" + model.getValue());
}
}
}
package demo1130;
//实体类
//试题
public class QuestionModel {
private int id;// 题号
private String title;// 题目
private String optionA;// 选项A
private String optionB;// 选项B
private String optionC;// 选项C
private String optionD;// 选项D
private String answer;// 答案
private int value;// 分值
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
这段代码可以分为以下几个部分:
-
包名和导入语句:
package demo1130; // 导入所需的类
-
QuestionModel
类的定义:public class QuestionModel { // 类的成员变量 private int id; private String title; private String optionA; private String optionB; private String optionC; private String optionD; private String answer; private int value; // 类的方法 // 省略 getter 和 setter 方法 }
-
QuestionModel
类的成员变量和 getter/setter 方法的定义:public int getId() { return id; } public void setId(int id) { this.id = id; } 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; } public int getValue() { return value; } public void setValue(int value) { this.value = value; }
以上是整段代码的分块解析。这段代码定义了一个名为 QuestionModel
的实体类,用于表示试题的模型。该实体类具有题号、题目、选项和答案等属性,并提供了对应的获取和设置方法。这个实体类可以作为试题系统中试题数据的载体,在处理试题相关的逻辑时使用。