当前位置:文档之家› 在线考试系统__简单java代码

在线考试系统__简单java代码

Online Exam Questions()
***************************************************************Index.java
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Index {
static int adminMenu() {
int choice = 0;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
System.out.println("1 . Register Qustions");
System.out.println("2 . Start Exam");
System.out.println("3 . Get out");
choice = Integer.parseInt(br.readLine());
} catch (Exception e) {
System.out.println("Enter is error");
}
return choice;
}
public static void main(String[] args) {
System.out.println("Welcome to Online Exam System");
int choice;
do {
choice = adminMenu();
switch (choice) {
case 1:
Questions br = new Questions();
br.QuestionList();
break;
case 2:
Questions temp = new Questions();
temp.exam();
break;
}
} while (choice != 3);
System.out.println("Thank You ");
}
}
*******************************************************questions.java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Random;
public class Questions {
int questionNumber;
String question;
String optionA;
String optionB;
String optionC;
String optionD;
String correctOption;
static int Qnumber;
static ArrayList a1 = new ArrayList(10);
boolean b = true;
void QuestionList() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
System.out.println("Enter QuestionNumber : ");
this.questionNumber = Integer.parseInt(br.readLine());
if (Qnumber != 0) {
for (int i = 0; i < Qnumber; i++) {
Questions q = (Questions) a1.get(i);
if (this.questionNumber == q.questionNumber) {
System.out.println("questionNumber is extist");
b = false;
break;
}
}
}
if (b) {
System.out.println("Enter Question : ");
this.question = br.readLine();
System.out.println("Enter Option-A :");
this.optionA = br.readLine();
System.out.println("Enter Option-B :");
this.optionB = br.readLine();
System.out.println("Enter Option-C :");
this.optionC = br.readLine();
System.out.println("Enter Option-D :");
this.optionD = br.readLine();
System.out.println("Enter correctAnswer :");
this.correctOption = br.readLine();
question();
}
} catch (Exception e) {
System.out.println("Enter handled the exception");
}
}
void question() {
Questions q1 = new Questions();
q1 = this;
a1.add(q1);
Qnumber++;
System.err.println("Register success");
System.out.println("Total Questions Registered = " + Qnumber);
}
void exam() {
if (Qnumber != 0) {
System.out.println("Start Exam");
if (Qnumber < 5) {
System.out.println("How many questions :" + (Qnumber));
} else {
System.out.println("How many questions : 5");
}
Random random = new Random();
HashSet h1 = new HashSet();
for (int i = 0; i < 5; i++) {
h1.add(random.nextInt(Qnumber));
}
Iterator iterator = h1.iterator();
while (iterator.hasNext()) {
Questions q1 = (Questions) a1.get((Integer) iterator.next());
System.out.print(q1.questionNumber + ".");
System.out.println(q1.question);
System.out.println("A :" + q1.optionA);
System.out.println("B :" + q1.optionB);
System.out.println("C :" + q1.optionC);
System.out.println("D :" + q1.optionD);
System.out.println("Enter CorrectOption : ");
try {
BufferedReader bReader = new BufferedReader(
new InputStreamReader(System.in));
String temp = bReader.readLine();
if (temp == q1.correctOption) {
System.out.println("Right");
} else {
System.out.println("Wrong");
}
} catch (Exception e) {
System.out.println("Enter handled the exception");
}
}
} else {
System.out.println("No Questions");
}
}
}。

相关主题