restart the loop

1

Hello, I am creating a game that I have as an exercise, it is to guess the number. The program works perfectly for me, but I need at the end of the game to ask if I want to play another game or not, that is, to come to the beginning of the loop. I do not know how to represent it the truth. . Let's see if you can lend me a hand thanks

import java.util. *;

import java.util.concurrent.ThreadLocalRandom;

public class game {

public static void main(String[] args) {


    Scanner entrada=new Scanner(System.in);
    System.out.println("Introduce los rangos");
    int rango1=entrada.nextInt();
    int rango2=entrada.nextInt();

    int aleatorio = ThreadLocalRandom.current().nextInt(rango1, rango2 + 1);

    int numero=0;
    int intentos=0;

    while(aleatorio!=numero){
        intentos++;
        System.out.println("Dime un numero");
        numero=entrada.nextInt();

    if (numero>100){

        }
        if (numero>aleatorio){
            System.out.println("Mas bajo");
        }
            if(numero<aleatorio){
                System.out.println("Mas alto");
            }
                if(numero==aleatorio){
                    System.out.println("Has acertado " + aleatorio);
                }


    System.out.println("Has necesitado " + intentos + "intentos");

    System.out.println("Juego terminado");

}     } }

This at the moment we have not yet given it: (

    
asked by user103944 18.10.2018 в 11:07
source

1 answer

2

You could create a static method in your class;

public class juego {


public static void main(String[] args) {
  boolean seguirJugando=false;

do{
 Sring respuesta="";
 jugar();
 system.out.printLn("¿Quieres seguir jugando?");
 respuesta=teclado.readLine();
 if(respuesta=="si"){
     seguirJugando=true;
 }else{
     seguirJugando=false;
 }
  } while(seguirJugando==true);



}

public static void jugar(){

Scanner entrada=new Scanner(System.in);
System.out.println("Introduce los rangos");
int rango1=entrada.nextInt();
int rango2=entrada.nextInt();
int aleatorio = ThreadLocalRandom.current().nextInt(rango1, rango2 + 1);
int numero=0;
int intentos=0;

while(aleatorio!=numero){
    intentos++;
    System.out.println("Dime un numero");
    numero=entrada.nextInt();

if (numero>100){

    }
    if (numero>aleatorio){
        System.out.println("Mas bajo");
    }
        if(numero<aleatorio){
            System.out.println("Mas alto");
        }
            if(numero==aleatorio){
                System.out.println("Has acertado " + aleatorio);
            }               
System.out.println("Has necesitado " + intentos + "intentos");
System.out.println("Juego terminado");

  }
 }//fin método estático
}//fin de la clase

I have not given you the exact answer since I could not prove the code, but the idea is there, I hope it works for you.

The idea is to take all your "game" to an external method and make use of it until the user enters a different answer for himself.

    
answered by 18.10.2018 в 11:25