Solucionado (ver solução)
Solucionado
(ver solução)
3
respostas

Desafio cumprido! Jogo da Adivinhação

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

public class Main {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        int tentativas = 0;
        int tentativasRestantes = 5;
        int numeroSecreto = new Random().nextInt(100);

        System.out.println("Tente adivinhar o número secreto entre 1 e 100, em 5 tentativas!");

        int verificarChute = 0;

        while (verificarChute != numeroSecreto) {
            System.out.println("Digite seu chute! ");
            verificarChute = scanner.nextInt();
            tentativas++;
            tentativasRestantes--;

            if (verificarChute == numeroSecreto) {
                System.out.println("Você descobriu o número secreto com " + tentativas + " tentativas!");
                break;
            } else if (verificarChute > numeroSecreto) {
                System.out.println("O número secreto é menor! Tente novamente! Restam " + tentativasRestantes + " tentativas!");
            } else {
                System.out.println("O número secreto é maior! Tente novamente! Restam " + tentativasRestantes + " tentativas!");
            }
            if (tentativasRestantes < 1) {
                System.out.println("Você esgotou o número de tentativas. Reinicie o jogo!");
                break;
            }
        }
    }
}
3 respostas

Tela preta com a saída do Jogo da Adivinhação, onde mostra que o jogador descobriu o número secreto com 3 tentativas.

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

public class Main {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        int tentativas = 0;
        int tentativasRestantes = 5;
        int numeroSecreto = new Random().nextInt(100);

        System.out.println("***************************************************************");
        System.out.println("                     JOGO DA ADVINHAÇÃO                        ");
        System.out.println("***************************************************************");

        System.out.println("Tente adivinhar o número secreto entre 1 e 100, em 5 tentativas!");

        int verificarChute = 0;

        while (verificarChute != numeroSecreto) {
            System.out.println("Digite seu chute! ");
            verificarChute = scanner.nextInt();
            tentativas++;
            tentativasRestantes--;

            if (verificarChute == numeroSecreto) {
                System.out.println("Você descobriu o número secreto com " + tentativas + " tentativas!");
                break;
            } else if (verificarChute > numeroSecreto) {
                System.out.println("O número secreto é menor! Tente novamente! Restam " + tentativasRestantes + " tentativas!");
            } else {
                System.out.println("O número secreto é maior! Tente novamente! Restam " + tentativasRestantes + " tentativas!");
            }
            if (tentativasRestantes < 1) {
                System.out.println("Você esgotou o número de tentativas. Reinicie o jogo!");
                break;
            }
        }
    }
}
solução!

Oii, Gilvaneide! Tudo bom?

Parabéns por ter chegado até aqui com o seu código do jogo da adivinhação! Você está no caminho certo, o código atende ao que foi pedido na atividade e o fato de você estar controlando o número de tentativas, o que é ótimo!

Uma dica é na mensagem inicial, onde você menciona que o número está entre 1 e 100, mas o intervalo correto é entre 0 e 100. Ajuste a mensagem para refletir isso e para ficar intuitivo.

Continue se dedicando aos estudos como está fazendo, você vai muito longe!

Qualquer dúvida, compartilhe com a gente para podermos te auxiliar.

Alura Conte com o apoio da comunidade Alura na sua jornada. Abraços e bons estudos!
//Código corrigido
import java.util.Random;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        int tentativas = 0;
        int tentativasRestantes = 5;
        int numeroSecreto = new Random().nextInt(100);

        System.out.println("***************************************************************");
        System.out.println("                     JOGO DA ADVINHAÇÃO                        ");
        System.out.println("***************************************************************");

        System.out.println("Tente adivinhar o número secreto entre 0 e 100, em 5 tentativas!");

        int verificarChute = 0;

        while (verificarChute != numeroSecreto) {
            System.out.println("Digite seu chute! ");
            verificarChute = scanner.nextInt();
            tentativas++;
            tentativasRestantes--;

            if (verificarChute == numeroSecreto) {
                System.out.println("Você descobriu o número secreto com " + tentativas + " tentativas!");
                break;
            } else if (verificarChute > numeroSecreto) {
                System.out.println("O número secreto é menor! Tente novamente! Restam " + tentativasRestantes + " tentativas!");
            } else {
                System.out.println("O número secreto é maior! Tente novamente! Restam " + tentativasRestantes + " tentativas!");
            }
            if (tentativasRestantes < 1) {
                System.out.println("Você esgotou o número de tentativas. Reinicie o jogo!");
                break;
            }
        }
    }
}