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...
Expand|Select|Wrap|Line Numbers
- 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);
- }
- }
- }