0

Hi I am having issues making a quiz program in java, here is what I have: (note: I have not set questions for Q3-10). The issue I am having is once the user enters the first question correctly, and the second question is displayed, if the user enters in the correct answer for the second question: a^2+b^2=c^2, then "Sorry, you lost, your score is 1/10 is still displayed". Even though it is supposed to be: "Correct! Next Question:" I have just started coding so sorry if this a easy mistake.

    import java.util.Scanner;

    public class Quiz {

public static void main(String[] args) {
    int Q1 = 0, Q3, Q4, Q5, Q6, Q7, Q8, Q9, Q10 = 0;
    String Q2 = "";



    Scanner in = new Scanner(System.in);
    Scanner st = new Scanner(System.in);


    System.out.println("Welcome to the math quiz!, Type Reset at any time to start over");
    System.out.println("Q1: What is the square root of 9?");





        //beggining of questions:
    while (Q1 != 3) {
        Q1 = in.nextInt();
    if ( Q1 == 3) {
        System.out.println("Correct! Next Question:");
        System.out.println("Q2: What is pythagreoum's therom?");

    } else {
        System.out.println("Sorry, you lost, your score is 0/10");
    }
    }
  //end of Q1



    while (Q2 != "a^2+b^2=c^2" ) {
        Q2 = st.nextLine();
    if ( Q2 == "a^2+b^2=c^2" ) {
            System.out.println("Correct! Next Question:");  
    } 
    else {
        System.out.println("Sorry, you lost, your score is 1/10");
    }
    }

    //end of Q2 
            }
            }
tshepang
  • 12,111
  • 21
  • 91
  • 136

4 Answers4

0

Try Q2.equals("a^2+b^2=c^2").

String is an object, so Q2 is a pointer. Comparing Q2 to anything else compares the value in the pointer (which is the memory location of the String. Q2 == //something will only return true if //something is ALSO the memory address of Q2 (two pointers pointing to the same object). As such, you need to use the equals() method when comparing Strings in Java.

And not that this has anything to do with the specific problem you're asking, but your program will count me wrong for typing other correct answers like c^2=a^2+b^2, or even something like A^2+B^2=C^2. Might be something to consider before you call this project finished.

nhgrif
  • 61,578
  • 25
  • 134
  • 173
0

While comparing Strings you should be really careful when you use == operator. It will only return true if both of the two strings being compared are same(i.e. are same String objects). Use Q2.equals("a^2+b^2=c^2") . For more on difference between == and .equals read THIS

Community
  • 1
  • 1
Prateek
  • 1,916
  • 1
  • 12
  • 22
0

For Q2 it should read:

while (!Q2.equals("a^2+b^2=c^2")) {
    Q2 = st.nextLine();
    if (Q2.equals("a^2+b^2=c^2")) {
        System.out.println("Correct! Next Question:");
    } else {
        System.out.println("Sorry, you lost, your score is 1/10");
    }
}

Don't use "==" to compare two Strings.

Rafal Borowiec
  • 5,124
  • 1
  • 24
  • 20
0

You could store all your questions in a string array like String[] questions = new String[n], where n is the number of questions you want to store. You have to specify it. You could do the same for the answers. Notice that you can use String questions[] or String[] questions. You could do the same for the answers.

create variables

String[] questions=new String[10];
String[] answers = new String[10];//make all answers upper case
questions[0]="Who solved fermat's last problem a^n+b^n=c^n where n>2?";
answers[0]="ANDREW WILES.";//fill up both arrays with 10 q's and a's
Scanner input=new Scanner(System.in);
String response;String rtoup;
Boolean useranswer;
int qlength=0;//the number of questions in the questions array
int score=0;//the user hasn't answered any questions yet
//Then could write a while for loop (I prefer the for loop)

for(int i=0;i<qlength;i++){
     System.out.println(questions[i]);
     response=input.Next();rtoup=response.toUpperCase();
   if(rtoup.equals(answers[i])){
     useranswer=true;score++;
  }
   if(useranswer==true)
   {
     System.out.println("Correct and your score is: "+score+"/10");
     }
   else 
     {
       System.out.println("False and your score is: "+score+"/10");
     }
}//end of for loop

Alternatively you can write all the questions and save them in some text file say file1, then you can have java read from that txt file line by line. Then you could write all the answers in another text file say file2.

public void static main(String[] args){
   File file1=new File("file1path");
   File file2=new File("file2path")
   Scanner input1=new Scanner(file1);
   Scanner input2=new Scanner(file2);
  while(input1.hasNext()&&input2.hasNext())
    {
      //compare user response to input2
    }//end while
}//end main

Not 100% sure about Fileuse in this context but this will at least give you some ideas

About .equals vs == If you still want to use ==, you could convert user input (assuming they're string numbers) to integers by using Integer.parseInt("2132");

gwrw
  • 139
  • 2
  • 12