Problem with if in Java

Asked

Viewed 90 times

-1

I’m trying to make a program like "lottery", locked in a mistake there on if. If you can help me, follow the code:

package praticando;

import java.util.Random;
import java.util.Scanner;


public class JogoNumeros {

    public static void main(String[] args) {
       Random aleatorio = new Random();
       int valor = aleatorio.nextInt(10) + 1;
       Scanner N = new Scanner (System.in);

        System.out.println("Faça sua aposta digitando um número de 1 até 10: ");
        int aposta = N.nextInt();
        System.out.println("Número sorteado|---> " + valor);

        if  N = valor {
            System.out.print(" $$$$ PARABÉNS, você ganhou na loteria!!! $$$$$");

        }else{
            System.out.println("Não foi desta vez, continue apostando!");
        }
    }
}
  • Hello, Thiago, you are using the wrong operator. For Assignment we use the same (=), for comparison we used (==), try and see if this is it. Logo in line if N = value tries if N == value ;

  • 7

    Take a look over how to ask questions

  • Hello Marcus Italo, you are right. I modified but not yet rolled.

  • @Marcusitalus N is a Scanner, it makes no sense to compare it with a int. What must be done is if (valor == aposta)

  • I modified it, but it’s not running yet.

  • Yes, it worked. Thank you. *

  • But the with a yellow bulb with exclamation. Thank you friend.

Show 2 more comments

1 answer

1

You had to compare with the variable aposta:

public static void main(String[] args) {

    Random aleatorio = new Random();
    int valor = aleatorio.nextInt(10) + 1;
    Scanner N = new Scanner(System.in);

    System.out.println("Faça sua aposta digitando um número de 1 até 10: ");
    int aposta = N.nextInt();
    System.out.println("Número sorteado|---> " + valor);
    if (aposta == valor) {
        System.out.print(" $$$$ PARABÉNS, você ganhou na loteria!!! $$$$$");

    } else {
        System.out.println("Não foi desta vez, continue apostando!");
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.