Java - Adivina un juego de números. El usuario tiene 6 intentos. He llegado tan lejos que cuando se ingresa el sexto intento, el juego simplemente se apaga
Frecuentes
Visto 2,455 equipos
-3
I am unable to get the program to loop around to the start from here and am unable to create the session to write each attempt to a high score text file and save the amount of attempts and the users best attempt.
I want the program to pop up another JOptionPane
box to say the user has used all of their attempts and also ask if they would like to play again so that it will loop back to the start. I also have to create a session where the user inputs their name at the start and each attempt in that session is written to a high score text file and saved.
Below is the coding I have got so far.
import javax.swing.JOptionPane;
import java.util.Random;
public class RandomNumberGuesser{
public static void main(String[] args) {
Random random = new Random();
int attempts = 0; // Number of attempts
int randomNumber = random.nextInt(100);
System.out.println(randomNumber);
// This outside the loop so is showed just ONE time
JOptionPane.showInputDialog(null,
"This program will generate a random number from 0 to 100 which you have to guess.\nNumber Guesser \nPlease enter your name."
,JOptionPane.QUESTION_MESSAGE);
while (true && attempts < 6) {
attempts++;
String guess = JOptionPane.showInputDialog(null, "Guess a number.",
"Guess", JOptionPane.QUESTION_MESSAGE);
if (guess == null) {
System.out.println("The user has terminated the program");
System.exit(0);
}
int guess1 = Integer.parseInt(guess);
if (guess1 > 100 || guess1 < 0)
JOptionPane
.showMessageDialog(
null,
"Guess is out of range!\nPlease enter valid input.",
"Invalid Input",
JOptionPane.WARNING_MESSAGE);
else if (randomNumber > guess1)
JOptionPane.showMessageDialog(null,
"You guessed too low.\nGuess again!", "Your guess",
JOptionPane.INFORMATION_MESSAGE);
else if (randomNumber < guess1)
JOptionPane.showMessageDialog(null,
"You guessed too high.\nGuess again!",
"Your guess", JOptionPane.INFORMATION_MESSAGE);
else {
JOptionPane
.showMessageDialog(null,
"You guessed the number right!\nIt took you "
+ attempts + " attempt(s) to guess it.",
"Congratulations!",
JOptionPane.INFORMATION_MESSAGE);
if (JOptionPane.showConfirmDialog(null,
"Want to play again?", "Play again?",
JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
System.out.println("Play again soon!");
System.exit(0);
} else {
randomNumber = random.nextInt(100);
System.out.println(randomNumber);
attempts = 0;
}
}
}
}
}
1 Respuestas
0
Based on the program it is evident that after the 6th attempt the loop exits and the program ends. How about opening an infinite loop ie while(true) and then witin the while loop have this as your first check
while(true){
if(attempts >= 6){
if (JOptionPane.showConfirmDialog(null,
"Want to play again?", "Play again?",
JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
System.out.println("Play again soon!");
System.exit(0);
} else {
randomNumber = random.nextInt(100);
System.out.println(randomNumber);
attempts = 0;
}
}
}
....Rest of your code goes here
}
contestado el 28 de mayo de 14 a las 14:05
I've never seen the System.exit(0) line. Is that the same as using break and why would you use one over the other? Not criticizing just curious. - gcalex5
@gcalex5 System.exit()
causes the JVM/JRE to exit. - awksp
@gcalex5 System.exit(0) ends the program witout an error. As such for this program inside the while loop even a break would suffice and the program would end. As there is no other execution code after the while loop. - ganaraj
@ganaraj with this in the loop is only allowing the user 1 guess and if it is incorrect it is telling the user it is too high or too low and then asking do they want to play again whereas if the number is correct it just exits the game without asking the user if they want to play again. - user3223512
Corrected, this is a check to ensure that when the max attempt is reached ie 6 ask the user whether he wants to continue. - ganaraj
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas java swing or haz tu propia pregunta.
And what's your question? - StanislavL
If the title has more useful information than the post you are not writting your question properly. Why are you unable to make it work? What is happening? What do you want to do? - DSquare
Oh, sorry, I want the program to pop up another JOptionPane box to say the user has used all of their attempts and also ask if they would like to play again so that it will loop back to the start. I also have to create a session where the user inputs their name at the start and each attempt in that session is written to a high score text file and saved. This is my first time posting to the website, I was unsure of what to include. - user3223512
and what's the purpose of while (true && attempts < 6) { ? just out of curiosity. - zubergu