while loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sonyb
    New Member
    • Mar 2008
    • 2

    #1

    while loop

    In this program , I informed the user to enter 3 random numbers .. Each one of these numbers were taken and entered into an function (2x + 5) and after the 3rd number was inserted into this equation , the user was asked to figure out what function was used to ahieve that answer.

    I have a while loop, that compares the selection to the function (2x + 5) . I gave the user 3 trys to figure it out , and if he failed on the 3rd round , I am going to insert more code. Why is it that while I am in the while loop and say on my second attempt in guessin the function I get right... But it still proceeds to the third attempt without telling me I'm correct.

    I am fairly new to this site as well as programming as a whole. Didn't think I would have ever been able to post something like this here, but I guess if your really passionate about something you have to start somewhere... Any sort of help or advise would be grateful..

    Thanks in advance...


    Code:
    import javax.swing.*;
    
    
    
    public class GuessingGameRevised
    
    {
    
    	public static void main (String [] arguments)
    
    	{
    
    
    
    	String   guess1, guess2, guess3, selection;
    	String   function1 = "2x + 5";
    
    	double  answer1,answer2,answer3;
    
    	int counter = 1;
    
    
    
    
    	guess1 = JOptionPane.showInputDialog(null, " Please select a number " );
    	guess2 = JOptionPane.showInputDialog(null, " Please select a second number " );
    	guess3 = JOptionPane.showInputDialog(null, " Please select a third number " );
    
    
    
    
    	answer1 = Double.parseDouble(guess1);
    	answer2 = Double.parseDouble(guess2);
    	answer3 = Double.parseDouble(guess3);
    
    
    
    	selection = JOptionPane.showInputDialog(null, " You chose " + answer1 + "\nand when inserted into the function equals :\n" + function(answer1) +
    	" \n\nYou chose " + answer2 + "\nand when inserted into the function equals:\n" + function(answer2)+
    	" \n\nYou chose " + answer3 + "\nand when inserted into the function equals:\n" + function(answer3) +
    	"\n\nNow, can you determine what the function is?");
    
    	if(function1.equals(selection))
    		{
    			JOptionPane.showInputDialog(null, "That is correct ... Round two ...");
    
    		System.exit(0);
    
    
    		}
    
    
    
    	while (!function1.equals(selection) )
    	{
    
    
    
    	JOptionPane.showInputDialog(null, selection + " is not the right answer."
    								+ " Sorry try again.");
    
    	counter++;
    
    
    
    	if(counter >=3)
    
    		{
    			JOptionPane.showMessageDialog(null, " Please study more ... Goodbye" );
    			System.exit(0);
    		}
    
    
    	}
    
    
    
    
    	}
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    Basically, your code isn't working because the logic is wrong.

    If you step through your code by hand this should be evident.

    For example, if the first guess is correct you display "That is correct ... Round two ...". Now what happens if the first guess is not correct? Control flow moves past this first if statement to the while loop, correct? Once in the while loop there are two possible outputs:

    "... is not the right answer. Sorry try again."
    or
    " Please study more ... Goodbye"

    Note there is no output saying "correct". Therefore there is no output that can say "correct". Do you see the faulty logic?

    Comment

    Working...