QuizTest.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1;
/**
*
* @author rajani sharma
*/
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class QuizTest {
public static void main (String[] args) throws IOException{
String question = "";
String choiceA = "";
String choiceB = "";
String choiceC = "";
String choiceD = "";
String answer = "";
String line;
String userAnswer;
int i = 0;
BufferedReader reader = null;
reader = new BufferedReader(new FileReader("Caitto.txt"));
Scanner scan = new Scanner(System.in);
QuizClass run = new QuizClass(i, question, choiceA, choiceB, choiceC, choiceD, answer);
while((line = reader.readLine()) != null){
//Finds the question
if(line.contains("?")){
run.question = line;
System.out.println(run.getQuestion() + "\n");
}
//Finds answer "A"
if(line.contains("A.")){
run.choiceA = line;
System.out.println(run.getChoiceA());
}
//Finds answer "B"
if(line.contains("B.")){
run.choiceB = line;
System.out.println(run.getChoiceB());
}
//Finds answer "C"
if(line.contains("C.")){
run.choiceC = line;
System.out.println(run.getChoiceC());
}
//Finds answer "D"
if(line.contains("D.")){
run.choiceD = line;
System.out.println(run.getChoiceD() + "\n");
}
//Finds the correct answer for the question
if(line.contains("Correct Answer:"))
{
//System.out.println("correct answer block");
String[] a = line.split(":");
answer = a[a.length - 1];
run.answer = answer;
// System.out.print(" Answer:"+answer);
System.out.print("Your Answer:");
userAnswer = scan.next();
//Checks if the user's input matches the correct answer from the file
if(userAnswer.equalsIgnoreCase(answer)){
System.out.println("Correct!\n");
i++;
}
//Checks if the user's input doesn't match the correct answer from the file
else if (!userAnswer.equalsIgnoreCase(answer)) {
System.out.println("Wrong, the correct answer was: " + run.getAnswer());
i++;
}
}
}
if(i == 274){
reader.close();
scan.close();
}
}
}
QuizClass.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1;
/**
*
* @author rajani sharma
*/
public class QuizClass {
String question, answer, choiceA, choiceB, choiceC, choiceD;
private int questionNum;
public QuizClass(int questionNum, String question, String choiceA, String choiceB, String choiceC, String choiceD, String answer){
this.question = question;
this.answer = answer;
this.choiceA = choiceA;
this.choiceB = choiceB;
this.choiceC = choiceC;
this.choiceD = choiceD;
}
public String getQuestion(){
return question;
}
public String getAnswer(){
return answer;
}
public String getChoiceA(){
return choiceA;
}
public String getChoiceB(){
return choiceB;
}
public String getChoiceC(){
return choiceC;
}
public String getChoiceD(){
return choiceD;
}
public int getQuestionNum(){
return questionNum;
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1;
/**
*
* @author rajani sharma
*/
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class QuizTest {
public static void main (String[] args) throws IOException{
String question = "";
String choiceA = "";
String choiceB = "";
String choiceC = "";
String choiceD = "";
String answer = "";
String line;
String userAnswer;
int i = 0;
BufferedReader reader = null;
reader = new BufferedReader(new FileReader("Caitto.txt"));
Scanner scan = new Scanner(System.in);
QuizClass run = new QuizClass(i, question, choiceA, choiceB, choiceC, choiceD, answer);
while((line = reader.readLine()) != null){
//Finds the question
if(line.contains("?")){
run.question = line;
System.out.println(run.getQuestion() + "\n");
}
//Finds answer "A"
if(line.contains("A.")){
run.choiceA = line;
System.out.println(run.getChoiceA());
}
//Finds answer "B"
if(line.contains("B.")){
run.choiceB = line;
System.out.println(run.getChoiceB());
}
//Finds answer "C"
if(line.contains("C.")){
run.choiceC = line;
System.out.println(run.getChoiceC());
}
//Finds answer "D"
if(line.contains("D.")){
run.choiceD = line;
System.out.println(run.getChoiceD() + "\n");
}
//Finds the correct answer for the question
if(line.contains("Correct Answer:"))
{
//System.out.println("correct answer block");
String[] a = line.split(":");
answer = a[a.length - 1];
run.answer = answer;
// System.out.print(" Answer:"+answer);
System.out.print("Your Answer:");
userAnswer = scan.next();
//Checks if the user's input matches the correct answer from the file
if(userAnswer.equalsIgnoreCase(answer)){
System.out.println("Correct!\n");
i++;
}
//Checks if the user's input doesn't match the correct answer from the file
else if (!userAnswer.equalsIgnoreCase(answer)) {
System.out.println("Wrong, the correct answer was: " + run.getAnswer());
i++;
}
}
}
if(i == 274){
reader.close();
scan.close();
}
}
}
QuizClass.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1;
/**
*
* @author rajani sharma
*/
public class QuizClass {
String question, answer, choiceA, choiceB, choiceC, choiceD;
private int questionNum;
public QuizClass(int questionNum, String question, String choiceA, String choiceB, String choiceC, String choiceD, String answer){
this.question = question;
this.answer = answer;
this.choiceA = choiceA;
this.choiceB = choiceB;
this.choiceC = choiceC;
this.choiceD = choiceD;
}
public String getQuestion(){
return question;
}
public String getAnswer(){
return answer;
}
public String getChoiceA(){
return choiceA;
}
public String getChoiceB(){
return choiceB;
}
public String getChoiceC(){
return choiceC;
}
public String getChoiceD(){
return choiceD;
}
public int getQuestionNum(){
return questionNum;
}
}
No comments:
Post a Comment