Results 1 to 4 of 4
  1. #1
    PVL268 is offline Member
    Join Date
    Feb 2009
    Posts
    27
    Rep Power
    0

    Default i can't see the mistake

    The task that I want to accomplish is after the program executes the user is prompt if he/she want to play again. I want to do it by using a second while loop but it does not work and can't see the mistake. The last line is unfinished, so that's not it.

    The way I did it and posted here ( with a little change in the Boolean variable or the string ) the program either executes repeatedly or ends after prompting the use for another game regardless of the answer.



    WHAT AM I DOING WRONG ???????? PLS HELP


    Java Code:
    import javax.swing.*;
    import java.util.*;
    
    public class CH05EX15_Original
    {
    
    static Scanner console = new Scanner (System.in);
    
    	public static void main(String[] args)
    	{
         String strGuess,input,cont;
    	 int num,guess,counter,diff;
         boolean done,oneMoreGame;
         guess = 0;
         num = (int)(Math.random()* 100);
         done = false;
         oneMoreGame = false;
         counter = 0;  
         cont = "yes";
         input = "exit";
         oneMoreGame = false;
         							
         									
     while (!oneMoreGame)    											
     {
         	while (!done)
         	{
         		strGuess = JOptionPane.showInputDialog("Enter an integer greater than or equal to 0 and" +
         				" less than a 100");
         		guess = Integer.parseInt(strGuess);
         		counter++;
         		diff = Math.abs(num - guess);
         		
         		if (guess == num)
         		{
         			JOptionPane.showMessageDialog(null,"You guessed the correct number in " + counter + " moves","YOU WON",
         					JOptionPane.INFORMATION_MESSAGE);
         		
         			done = true;
         			
         		//	JOptionPane.showInputDialog("Would you like to play again ?\n" +
         			//		"for YES press \"OK\"\n" + "For NO type \"exit\"");
         					
         						    						
         		}
         		else
         			if (guess < num)
         			{	//JOptionPane.showMessageDialog(null,"Your guess is lower than the number.\n" +
         					//	"Please guess again","SORRY",JOptionPane.INFORMATION_MESSAGE);
         				if (diff>=50)
    
    							JOptionPane.showMessageDialog(null,"Less than 50","SORRY",
    									JOptionPane.INFORMATION_MESSAGE);
    
    						
    						else if (diff>=30)
    							JOptionPane.showMessageDialog(null,"Less than 30","SORRY",
    									JOptionPane.INFORMATION_MESSAGE);
    							     							
    						else if (diff>=15)
    						
    							JOptionPane.showMessageDialog(null,"Less than 15","SORRY",
    									JOptionPane.INFORMATION_MESSAGE);
    							
    						
    						
    						else
    							
    							JOptionPane.showMessageDialog(null,"Less than 0","SORRY",
    									JOptionPane.INFORMATION_MESSAGE);
    			
         						
         			}				
         				  						
      			
         				
         			else
         			//	JOptionPane.showMessageDialog(null,"Your guess is higher than the number.\n" +
         				//		"Please guess again","SORRY",JOptionPane.INFORMATION_MESSAGE);
         			{
         						
         						
         							if (diff>=50)
    
         								JOptionPane.showMessageDialog(null,"More than 50","SORRY",
         										JOptionPane.INFORMATION_MESSAGE);
    		
         							
         							else if (diff>=30)
         								JOptionPane.showMessageDialog(null,"More than 30","SORRY",
         										JOptionPane.INFORMATION_MESSAGE);
         								     							
         							else if (diff>=15)
         							
         								JOptionPane.showMessageDialog(null,"More than 15","SORRY",
         										JOptionPane.INFORMATION_MESSAGE);
         								
         							
         							
         							else
         								
         								JOptionPane.showMessageDialog(null,"More than 0","SORRY",
         										JOptionPane.INFORMATION_MESSAGE);
         			}	
         	
         	}
         		
         	JOptionPane.showInputDialog("Would you like to play again?\n" +
         			"If YES press \"OK\" !!!\n" + "If NO type \"exit\" !!!");
         	
     
              
     if (input.equals("exit"))
         oneMoreGame = true;
     
     
     
     } 
    	}
    	
    }

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    10

    Default

    You never process the input from JOptionPane, so "input" always stays "exit", and out you go.

    In general, I would suggest using showConfirmDialog() and use the YES_NO option. That leaves no room for user typos, and they don't have to type. Regardless, you have to look at the response from JOptionPane.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    28

    Default

    this is a double post: http://www.java-forums.org/new-java/...e-mistake.html

    likely accidental.

  4. #4
    PVL268 is offline Member
    Join Date
    Feb 2009
    Posts
    27
    Rep Power
    0

    Default

    Thank you for the suggestion, I'll try it right away.
    Sorry for doubling the post. My bad.

Similar Threads

  1. i can't see the mistake
    By PVL268 in forum New To Java
    Replies: 2
    Last Post: 04-28-2009, 06:30 AM
  2. PLEASE!!!help me to find mistake
    By sasha20 in forum New To Java
    Replies: 1
    Last Post: 01-11-2008, 11:50 AM
  3. Newbie can't spot his mistake :(
    By naxalyte in forum New To Java
    Replies: 4
    Last Post: 11-21-2007, 08:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •