Java game using Random,Boolean,Scanner,while,if/Else

Asked

Viewed 146 times

-4

The idea of the game is to use a Random from 1 to 100 and the user to hit, when the number of the user is higher, send a message warning the user, in the same way if it is smaller and giving more chances for the user to hit.

The code itself is not compiling and I’m not finding the error

public static void main(String[] args) {

    Random rand = new Random();
    int numeroSorte = rand.nextInt(100) + 1;

    System.out.println("Adivinhe o numero que estou pensando, ele esta entre 1 e 100");

    boolean continuar = true;

    Scanner scan = new Scanner(System.in); 
    while (continuar) {

        System.out.print("digite um numero ");
        int numeroUsuario = Integer.valueOf(scan.next());

        if (numeroUsuario == numeroSorte) {
            System.out.println("Parabens Voce acertou o numero ! ^^ ");
            continuar = false;

        } else if (numeroUsuario < numeroSorte) {
            System.out.println("o numero " + numeroUsuario + "é menor que o meu numero");
        } else {
            System.out.println("o numero " + numeroUsuario + "é maior que o meu numero");
        }
    }
    scan.close();

}
}
  • 3

    Please read this: https://pt.meta.stackoverflow.com/a/5485/28595

  • 1

    The proxy on the network here blocks photos from most photo sharing domains, such as Imgur (used by the stack overflow), post the code if not me and a lot of people won’t be able to see anything. Another thing, seeks to write correctly, Your use of c as being se made me confuse twice the reading, no need to be an expert in Portuguese, but avoid unnecessary abbreviations.

  • 1

    You have not imported the required classes. You are also not using the operator new correctly

  • Thanks for the help, I fixed the error of using the photo, in the next questions I will not use photo

1 answer

0


It would be a lot easier if I could copy the code, change it and put the corrected version in this answer, but you posted the code as a damn image and so it gets hard.

The problem is that random rand = newrandom(); should be Random rand = new Random();. Pay attention to the capital letters and the space.

You also forgot the imports of Random and of Scanner.

You also seem to have placed your class in the wrong folder or with an incorrect package declaration.

Use the instruction break; instead of a variable continuar.

scan.close(); is not something that makes sense.

  • Thank you for your help, I will do this, and I fixed the error in the image and put the code, and I will no longer make that mistake of using the photo.

Browser other questions tagged

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