So i have to make a program kind of game, is a guessing number game so I had made all the program but i want to tell to the user that you only he have 5 opportunities to guess the number, if the user doesnt guess it in 5 opportunities print a message saying like "JAJAJAJA Try again" i dont know how add opportunities.Somethings are in spanish because im from Spain so thats why. OHH that program have a menu the first option is the instruction the 2 is the game and the 3 is exit im only missing adding opportunities to the program like if the user have certain life that will be 5. Sorry for my grammar chico means like little or lower and grande means big
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class juego {
public static void main(String[] args) throws IOException {
proyecto obj = new proyecto();
int w = (int) (5000 * Math.random() + 1);
String x;
int y;
do {
System.out.println("MENU");
System.out.println("1.Instrucciones del juego");
System.out.println("2. Juego");
System.out.println("3. Salir");
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
y = Integer.parseInt(br.readLine());
switch (y) {
case 1:
obj.instu();
break;
case 2:
obj.proce(w);
break;
case 3:
System.out.println("GRACIAS POR JUGAR");
break;
default:
System.out.println("La opcion seleccionada no es valida");
}
} while (y != 3);
}
}
class proyecto {
void instu() {
System.out.println("Instrucciones");
System.out
.println("El juego se trata de adivinar el numero, tendras 10 intentos para poder adivinar");
System.out.println("Crees que podras ganar?");
}
void proce(int w) throws IOException {
int e;
System.out.println("COMENCEMOS!!!!");
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
System.out.println("INSERTE EL NUMERO");
{
do {
e = Integer.parseInt(br.readLine());
if (e < w) {
System.out.println("EL numero insertado es CHICO");
System.out.println("Inserte un numero mas grande");
} else {
System.out.println("EL numero insertado es muy grande");
}
{
if (e == w) {
System.out.println("GANASTE");
}
}
} while (e != w);
}
}
}
(Only giving hints - assuming this is homework, you should definitely work out the details for yourself.)
Currently your loop structure is:
do {
...
} while (e != w);
So you're going to loop until the player has guessed the answer. You need to loop until the player has guessed the answer or they've run out of guesses.
If you want to keep your current loop structure, you probably want to:
Alternatively, you could change your loop to a for
loop:
for (int guess = 0; guess < 5; guess++) {
...
}
... and break out of the loop if the player gets the right answer.
See more on this question at Stackoverflow