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.

Recommended Answers

All 3 Replies

for (guesses=0; guesses <= ichances; guesses++);

The semi-colon [;] at the end of the for loop makes this happen, remove it.

One, remove that ";" at the end of the for loop declaration. That ";" ends the for loop.

Also, near the end of the block that is suppossed to be the for loop, remove that guesses++; . Using that statement you would be incrementing "guesses" by two through every iteration of the loop, as you are also incrementing it as part of the loop declaration.

Also, remove that int guesses; before the for loop and declare the for loop as for (int guesses=0; guesses < ichances; guesses++) . Notice that the int is declared in the for loop (since it not used outside of it this will restrict to the for loop), and use simply "<", not "<=", or you are giving them an "extra" guess.

Wow thanks for the quick reply, guys.

Ahh that's what was doing it.
And nice eye seeing that guesses++ and the declaration of guesses outside the loop, masijade, it's there because it used to be a while loop instead of a for loop. But thanks that saved me a whooole lot of trouble

:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.