1

I was bored today so i thought i would go back to learning to code in Java. I thought of making a easy math quiz program where you have two random numbers that you need to multiply with each other.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;

public class Main {


public static void main(String args[]) throws IOException{
    Random random = new Random();
    int n1 = random.nextInt(30) + 5;
    int n2 = random.nextInt(40) + 5;
    int realanswer = n1 * n2;
    String realanswer2 = Integer.toString(realanswer);
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Answer these math questions:");
    System.out.println("");
    System.out.println(Integer.toString(n1) + " * " + Integer.toString(n2));
    String answer = br.readLine();
    if(answer == realanswer2){
        System.out.println("CORRECT");
    }else{
        System.out.println("WRONG");
        System.out.println("Answer is " + realanswer2);
    }

}

}

My problem is that even if i write the right answer it says its wrong. I think the problem lies in the transition from int to String.

Any help appreciated.

Herman Neple
  • 156
  • 12

0 Answers0