Hello. This is my first time programming in any language so please excuse my ignorance. I am trying write a program that asks the user to enter any number 3 times . I have assigned a function that takes the number and outputs an answer . After the 3rd number has been entered , the user is asked to guess what function was used to achieve that answer. The user is given 3 attempts to guess this answer , at which time I wan to move on to the next question. I am having trouble with the counter aspect of this program. Thanks for any assistance.

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, userselection;

    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?");




    while(!function1.equals(selection))

    {
        JOptionPane.showInputDialog(null, selection + " is not the correct function.\n\nSorry please try again");
        counter++;


        System.exit(0);

    }



        JOptionPane.showMessageDialog(null, "Wow your smarter than Dr. Nii!");

        System.exit(0);






    }

    public static double function(double f)
        {

            double func;
            func = (2*f) + 5;
            return func;

        }







}

while(!function1.equals(selection))
{
JOptionPane.showInputDialog(null, selection + " is not the correct function.\n\nSorry please try again");
counter++;
System.exit(0);
}

for starters, get rid of that System.exit(0); statement.
haven't read the rest, so there might be some other errors in it.
you might want to consider putting your code in code-tags next time, makes it easier for us to read it

You want to say "while the guess is not correct and counter is <= 3, do something" right? You need to change your line that reads:

while( ! function1.equals( selection ))

to:

while( ! function1.equals( selection ) && counter <= 3 )

The && means "and". This means that both statements must be true in order for the code inside the while loop to execute.

Another handy one is || which means "or", meaning that either statement can be true (or both) for the code inside the loop to execute.

EDIT: Too slow, and I didn't see the System.exit() statement...

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.