OK. So this program is supposed to prompt the user whether they want to play the guessing game.
If they say no then end the program otherwise it will start the game and ask them for their first name, last name and the number of guesses they want.
Then it creates a random number from 0-100 then the player tries to guess it.
If they guess it display a message and ask them if they want to play it again.
If they fail to get display a message and ask if they want to play again. Anyway needless to say my codes not finished :P but my loops seem to be screwed up. I'm quite new at this so I don't know why its jumping to the statements outside the for loop. Here is the code
import java.util.*;
import javax.swing.*;
public class GuessNumber
{
public static void main (String[]args)
{
String determineStart=JOptionPane.showInputDialog("Do you want to play the guessing game? (Y/N)");
while (determineStart.equalsIgnoreCase("y"))
{
String fName=JOptionPane.showInputDialog("Enter your first name");
String lName=JOptionPane.showInputDialog("Enter your last name");
String chances=JOptionPane.showInputDialog("How many guesses do you want?");
int ichances=Integer.parseInt(chances)-1;
int randomNumber=(int)(Math.random()*100)+1;
int guesses;
for (guesses=0; guesses <= ichances; guesses++);
{
String guessNum=JOptionPane.showInputDialog("Guess a number between 1 and 100");
int pNum=Integer.parseInt(guessNum);
if (pNum == randomNumber)
{
int index=0;
fName=fName.toUpperCase();
lName=lName.toUpperCase();
char charFName=fName.charAt(index);
String fullName=(charFName+" "+lName);
fullName=fullName.toUpperCase();
guesses=(ichances*ichances);
JOptionPane.showMessageDialog(null, "Congratulations "+fullName+ "\nYou got it!");
determineStart=JOptionPane.showInputDialog("Do you want to play the guessing game? (Y/N)");
}
JOptionPane.showMessageDialog(null,"That is not correct. Sorry.");
guesses++;
}
JOptionPane.showMessageDialog(null,"The number was "+randomNumber);
determineStart=JOptionPane.showInputDialog("Do you want to play the guessing game? (Y/N)");
}
}
}
Does anyone know why this is happening. I've highlighted the part that it skips to where I don't want it to.