jeudi 26 septembre 2019

If-statement using Same as operand coming back false

I am in the process of making a match game. I have two variables with a string of integers that I am using in an if-statement with the == operand. It shows a number then when I type back the same number it should print out a String. I type back the same number and even use 'System.out.println' to make sure my variables are correct but it always prints back the 'Else' String. Here is the script I am using.

package StackOverflow;
import java.util.Scanner;
import java.util.Random;
public class test1 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

    {
        Random r = new Random();
        Scanner s = new Scanner(System.in);
        int b = r.nextInt(9) + 1;
        int c = r.nextInt(9) + 1;
        String d ="" + b + c;
        System.out.println(d);
        String guess = "" + s.nextLine();
        System.out.println(guess);
        if(d == guess)
        {
            System.out.println(":)" );
        }
        else
        {
            System.out.println("almost");
        }
    }
}
}

Any comments are ideas are much appreciated.

Aucun commentaire:

Enregistrer un commentaire