Rslow
Rslow

Reputation: 67

Java getting a final score total of correct answers + boolean repeat

Trying to figure out how to print a score total of all the correct answers, pretty basic, also trying to include a boolean repeat function so I can ask the user to either "play again" or "quit".

import java.util.Scanner;
import java.util.Random;
public class GameVer1 {
public static void main(String []args) {




String inputName;
System.out.print("Enter your name here...");
inputName = userInput.nextLine();
System.out.print("Hi "+ inputName + ", Welcome!");

//boolean repeat = true;

        int tries = 0;
        int score = 0;
        int count = 0;


    do{
        Scanner userInput = new Scanner(System.in); 
        Random randomNumberChooser = new Random();

        int ranNum1;
        ranNum1 = randomNumberChooser.nextInt(12)+2;
        int ranNum2;
        ranNum2 = randomNumberChooser.nextInt(12)+2;
        int response = (ranNum1 * ranNum2);

        String question;
        System.out.println("How much is " + ranNum1 +" x " + ranNum2 + "?");
        question = userInput.next();
        tries = tries + 1;

        if(userInput == response){
        System.out.print("Correct!");
        userInput = response;
        count = count + 1;
    }



}while(tries <10);
}

}

Upvotes: 1

Views: 1052

Answers (1)

dub stylee
dub stylee

Reputation: 3342

You will need to first convert the input to an int for comparison, like so:

int num = userInput.nextInt();

You will then be able to compare if (num == response) { and you will no longer get the error.

EDIT

Also, as far as I can tell, you can remove the line userInput = response; as it would also give you an error, but even so, the userInput is read again at the next iteration of the loop.

Upvotes: 1

Related Questions