vendredi 20 septembre 2019

Make if == statement with Scanner nextInt

I am trying to make a match game where random numbers will show on the console then you have to type back the same numbers. I am having a problem with the if statement where it shows incorrect even when I input the right numbers. Here is my code so far:

package MatchGame;
import java.util.Random;
import java.util.Scanner;
public class match2 {

    int a;
    int b;
    int c;
    int d;

    String countdown[] = {
        "3...",
        "2...",
        "1..."
        };

public match2() throws InterruptedException
    {
    set1();
    }

public void set1() throws InterruptedException
    {
    Scanner s = new Scanner(System.in);
    System.out.println("press ENTER for your first set...");
    s.nextLine();

    for(int i = 0; i < countdown.length; i++)
    {
        Thread.sleep(1000);
        System.out.println(countdown[i]);
    }

    a = number();
    b = number();
    c = number();
    d = number();

    System.out.print(a);
    System.out.print(b);
    System.out.print(c);
    System.out.print(d);
    int set = a + b + c + d;


    int guess = s.nextInt();
        {
        if(set == guess)
        {
            System.out.println("Nice bruh +1");
        }
        else 
        {
            System.out.println("Nope");
        }
    }   
}


public static int number()
{
    Random r = new Random();
    int match = r.nextInt(9) + 1;
    //rv = rv + match;
    return match;
}
}

Also the variable 'set' doesn't seem to include the variables a, b, c, and d in it. Thx for any help

Aucun commentaire:

Enregistrer un commentaire