So in this following program, I have created 3 sets of questions aka difficulties using switch case but, whenever I choose a difficulty, the program just skips the first User string input sequence and declares that the answer is incorrect but, it works normally for the rest of the string input sequence of the chosen difficulty. The last case (case 3) has only 1 question and as soon as I choose 3, the program runs without executing the input sequence and declares that the answer is wrong. Is there a way to solve this?
import java.util.*;
class Quiz{
public static void main(){
Scanner sc = new Scanner(System.in);
System.out.println("\u000c");
int wa = 0;
int s = 0;
System.out.println("|-------------------|");
System.out.println("|Welcome to the Quiz|");
System.out.println("|-------------------|");
System.out.println("Enter your name:");
String p = sc.nextLine();
System.out.println("Enter your age:");
int age = sc.nextInt();
System.out.println("Please choose difficulty:-");
System.out.println("Press 1 for Easy difficulty.");
System.out.println("Press 2 for Normal Difficulty.");
System.out.println("Press 3 for Hard difficulty.");
if (age <= 6){
System.out.println("|----------------------------|");
System.out.println("|Recommended difficulty: Easy|");
System.out.println("|----------------------------|");
}
else if (age <= 12 && age > 6){
System.out.println("|------------------------------|");
System.out.println("|Recommended difficulty: Normal|");
System.out.println("|------------------------------|");
}
else{
System.out.println("|----------------------------|");
System.out.println("|Recommended difficulty: Hard|");
System.out.println("|----------------------------|");
}
int ch = sc.nextInt();
switch (ch){
case 1:
System.out.println("You have chosen difficulty: Easy");
System.out.println("1. Who was the first female doctor of India?");
String a = sc.nextLine();
String b = "ANANDI BAI JOSHI";
boolean c = b.equalsIgnoreCase(a);
if (b.equalsIgnoreCase(a)){
System.out.println("Correct!");
s = s + 1;
}else{
System.out.println("Wrong!");
wa = wa + 1;
}
System.out.println("2. Who is the current president of India?");
String d = sc.nextLine();
String e = "RAMNATH KOVIND";
boolean f = e.equalsIgnoreCase(d);
if (e.equalsIgnoreCase(d)){
System.out.println("Correct!");
s=s+1;
}else{
System.out.println("Wrong! Try Again!");
wa = wa + 1;
}
System.out.println("3. Who is India's current finance minister?");
String g = sc.nextLine();
String h = "NIRMALA SITARAMAN";
boolean i = h.equalsIgnoreCase(g);
if (h.equalsIgnoreCase(g)){
System.out.println("Correct!");
s=s+1;
}else{
System.out.println("Wrong! Try Harder!");
wa = wa + 1;
}
System.out.println("4. Who won the gold medal for javelin throw in the 2021 Olympics?");
String j = sc.nextLine();
String k = "NEERAJ CHOPRA";
boolean l = k.equalsIgnoreCase(j);
if (k.equalsIgnoreCase(j)){
System.out.println("Correct! Well Done!");
s=s+1;
}else{
System.out.println("Wrong!");
wa = wa + 1;
}
System.out.println("5. Who invented the Television?");
String m = sc.nextLine();
String n = "JOHN LOGIE BAIRD";
boolean o = n.equalsIgnoreCase(m);
if (n.equalsIgnoreCase(m)){
System.out.println("You are absolutely correct!");
s=s+1;
}else{
System.out.println("Wrong!");
wa = wa + 1;
}
System.out.println("Your score is: " +s);
System.out.println("Number of wrong answers: " +wa);
break;
case 2:
System.out.println("You have chosen difficulty: Normal");
System.out.println("1. What is the capital city of France?");
String r1 = sc.nextLine();
String t1 = "PARIS";
boolean u1 = t1.equalsIgnoreCase(r1);
if (t1.equalsIgnoreCase(r1)){
System.out.println("Brilliant!");
s = s+1;
}else{
System.out.println("Try harder!");
wa = wa + 1;
}
System.out.println("2. Which African country is very famous for chocolate?");
String v = sc.nextLine();
String w = "GHANA";
boolean x = w.equalsIgnoreCase(v);
if (w.equalsIgnoreCase(v)){
System.out.println("Outstanding!");
s = s+1;
}else{
System.out.println("Don't give up! Try again!");
wa = wa + 1;
}
System.out.println("3. Char Minar is located in which city?");
String a1 = sc.nextLine();
String b1 = "HYDERABAD";
boolean c1 = b1.equalsIgnoreCase(a1);
if (b1.equalsIgnoreCase(a1)){
System.out.println("Absolutely correct!");
s = s+1;
}else{
System.out.println("Wrong!");
wa = wa + 1;
}
System.out.println("4. Which is the largest state of India?");
String d1 = sc.nextLine();
String e1 = "RAJASTHAN";
boolean f1 = e1.equalsIgnoreCase(d1);
if (e1.equalsIgnoreCase(d1)){
System.out.println("Absolutely correct!");
s = s+1;
}else{
System.out.println("Wrong!");
wa = wa + 1;
}
System.out.println("5. Who was the first president of the United States?");
String g1 = sc.nextLine();
String h1 = "HYDERABAD";
boolean i1 = h1.equalsIgnoreCase(g1);
if (h1.equalsIgnoreCase(g1)){
System.out.println("Absolutely correct!");
s = s+1;
}else{
System.out.println("Wrong!");
wa = wa + 1;
}
System.out.println("Your score is: " +s);
System.out.println("Number of wrong answers: " +wa);
break;
case 3:
System.out.println("You have chosen difficulty: Hard");
System.out.println("What word in the English language contains these following:");
System.out.println("The fist two letters signify a male,");
System.out.println("the first three letters signify a female,");
System.out.println("the first four letters signify someone great,");
System.out.println("while the entire word signifies a great woman.");
System.out.println("What is the word?");
String j1 = sc.nextLine();
String k1 = "HEROINE";
boolean l1 = k1.equalsIgnoreCase(j1);
if (k1.equalsIgnoreCase(j1)){
System.out.println("YOU ARE THE RIDDLE MASTER!!!");
s = s+1;
}else{
System.out.println("Just stick to normal or easy difficulty.");
wa = wa + 1;
}
System.out.println("Your score is: " +s);
System.out.println("Number of wrong answers: " +wa);
break;
default:
System.out.println("Invalid Choice");
break;
}
}
}