1
resposta

[Projeto] Jogo de Adivinhação.

Pessoal, vou compartilhar com vocês a minha solução para o projeto do jogo de adivinhação. Caso tenham algo a acrescentar, fiquem à vontade.

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

public class GuessingGame {
    public static void main(String[] args) {
        // Initializing user's input scanner, random int number generator, Win/Lose boolean and user's choice variable.
        Scanner read = new Scanner(System.in);
        int randomNumber = new Random().nextInt(100);
        boolean userWon = false;
        int userChoice;

        // Writing and printing the game's intro text.
        String gameIntro = """
                -------------------------
                      GUESSING GAME
                -------------------------
                
                Welcome to our GUESSING GAME!
                
                We will generate a random int number for you.
                And you must guess this number!
                You have 10 tries.
                If you miss the number, we'll let you know
                if you're close or far!
                
                LET'S GO!
                """;
        System.out.println(gameIntro);
        System.out.println("Give it a try:");

        // Capturing the user's responses and giving back feedback to them depending on their answers.
        // The loop has 10 repetitions (10 tries for the user) according to the game's rules.
        for (int i = 0; i < 10; i++) {
            System.out.print(">>> ");
            userChoice = read.nextInt();

            if (userChoice == randomNumber) {
                userWon = true;
                break;
            } else if (userChoice >= randomNumber / 2 && userChoice < randomNumber) {
                System.out.println("You're quite close! Try again just a little bit higher!");
            } else if (userChoice < randomNumber / 2) {
                System.out.println("You're far. Score higher!");
            } else if (userChoice > randomNumber && userChoice <= randomNumber * 2) {
                System.out.println("Very close, a little bit lower!");
            } else if (userChoice > randomNumber * 2) {
                System.out.println("Too high! Try lowering a great bit.");
            }
        }

        // Printing Win or Lose messages according to the userWon boolean value.
        if (userWon) {  // User won.
            System.out.printf("%n%nYOU WON!%n------------------------");
        } else {        // User lost.
            System.out.printf("%n%nOh no! You ran out of tries! Start again and you'll make it :)%n------------------------");
        }
    }
}
1 resposta

Boa tarde Victor! Tudo bem com você?

Meus parabéns pela resolução da atividade. Testei o seu código e ele funcionou como esperado e atende muito bem a proposta da atividade, deixo abaixo um exemplo de uso:

GIF apresentando a execução do código do aluno

Conte com o apoio do Fórum na sua jornada. Fico à disposição.

Abraços e bons estudos!