QUOTE(jaden165 @ 4 Feb, 2008 - 05:43 AM)

The program below works fine, its just asking the user to guess a number between 1 and 1000. What I want to know is how to give the user only a fixed amount of guesses.
CODE
import javax.swing.*;
public class GuessingGame {
public static void main( String args[]) {
int value=1,num=0, maxGuesses = 3;
String a;
value = 1 + (int) (Math.random() * 1000 );
for (int i = 0; i < maxGuesses; i++)
while (num != value) {
a= JOptionPane.showInputDialog( "Product number");
num = Integer.parseInt( a );
if(num < value) {
JOptionPane.showMessageDialog(null,"Too low!");
}
else
if(num > value) {
JOptionPane.showMessageDialog(null,"Too High!");
}
else
if(num == value) {
JOptionPane.showMessageDialog(null,"Correct!");
}
}
}
}
You could define an int such as
int maxGuesses = 3; or something. Then have it increment each guess and have it exit the logic loop after the amount of guesses has been reached. Could even have a message that displays to the user how many guesses are left. There are several ways to do this I added a possible route to achieve this in your code quote:
CODE
int maxGuesses = 3;
//code
for (int i = 0; i < maxGuesses; i++)
//code....
--KYA
This post has been edited by KYA: 4 Feb, 2008 - 04:15 AM