Anonymous A.

asked • 07/20/20

Multiple choice quiz source code JAVA

I have implemented the following code. I just want the program to keep prompting the user until a valid response is entered. Can anyone help? I want that if user enters any other character other than A, B , C or D, the program ask the user to input valid option.


import java.io.*;

import java.util.*;


import java.util.Scanner;

public class Main {

static Scanner cin = new Scanner(System.in);

public static void main(String[] args){

int correct = 0;

int incorrect=0;

int questions = 5;

System.out.println("Welcome to the Multiple Choice Quiz! \n");


String[][] Ques_Ans = {

{"What is the size of float variable? ","\n A. 8 bit \n B. 16 bit \n C. 32 bit \n D. 64\n","C"},

{"Which one of these is not a primitive data type? ","\n A. integer \n B. char \n C. float \n D. double\n","B"},

{"Which data type is used to create a variable that should store text? ","\n A. myString \n B. string \n C. character \n D. String64\n","D"},

{"How do you create a variable with the numeric value 5? ","\n A. num x = 5; \n B. float x = 5; \n C. x = 5; \n D. int x = 5;\n","A"},

{"Which method can be used to find the length of a string in JAVA?","\n A. length() \n B. getlength() \n C. len \n D. getsize()\n","A"}};


String[] user_ans = new String[(int) questions];

int i=0;

do {

System.out.print("" + (i+1) + ". " + Ques_Ans[i][0] + " "+Ques_Ans[i][1]);

user_ans[i] = String.valueOf(cin.next().charAt(0));

isValid(user_ans);

if(Ques_Ans[i][2].equals(user_ans[i])) {

System.out.println("\n Correct!");

correct++;

}

else

{

System.out.println("\n Incorrect. The correct answer is "+Ques_Ans[i][2]);

incorrect++;

}


System.out.print("\n");

i++;

}while(i<questions));


System.out.println("\n Number of correct answers: "+ correct);

System.out.println("\n Number of incorrect answers: "+ incorrect);

System.exit(0);

}


}

2 Answers By Expert Tutors

By:

Brandon T. answered • 07/20/20

Tutor
5.0 (42)

Experienced Computer Science Tutor

Anonymous A.

where to put this piece of code? I have tried but the code stops after printing 1st question. import java.io.*; import java.util.*; import java.util.Scanner; public class Main { static Scanner cin = new Scanner(System.in); public static void main(String[] args) { int correct = 0; int incorrect = 0; int questions = 5; System.out.println("Welcome to the Multiple Choice Quiz! \n"); String[][] Ques_Ans = { { "What is the size of float variable? ", "\n A. 8 bit \n B. 16 bit \n C. 32 bit \n D. 64\n", "C" }, { "Which one of these is not a primitive data type? ", "\n A. integer \n B. char \n C. float \n D. double\n", "B" }, { "Which data type is used to create a variable that should store text? ", "\n A. myString \n B. string \n C. character \n D. String64\n", "D" }, { "How do you create a variable with the numeric value 5? ", "\n A. num x = 5; \n B. float x = 5; \n C. x = 5; \n D. int x = 5;\n", "A" }, { "Which method can be used to find the length of a string in JAVA?", "\n A. length() \n B. getlength() \n C. len \n D. getsize()\n", "A" } }; String[] user_ans = new String[(int) questions]; int i = 0; String USER = ""; do { System.out.print("" + (i + 1) + ". " + Ques_Ans[i][0] + " " + Ques_Ans[i][1]); user_ans[i] = String.valueOf(cin.next().charAt(0)); user_ans[i] = ""; do { user_ans[i] = USER; } while (user_ans[i] != "D" || user_ans[i] != "C" || user_ans[i] != "B" || user_ans[i] != "A"); if (Ques_Ans[i][2].equals(user_ans[i])) { System.out.println("\n Correct!"); correct++; } else { System.out.println("\n Incorrect. The correct answer is " + Ques_Ans[i][2]); incorrect++; } System.out.print("\n"); i++; } while (i < questions); System.out.println("\n Number of correct answers: " + correct); System.out.println("\n Number of incorrect answers: " + incorrect); System.exit(0); } }
Report

07/20/20

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.