/*please helpme! how can i get the number of times on how i attemp to get the correct answer!
please help me customize my inputbox
i am new to this!
this is a project of mine
please help:mrgreen: :mrgreen: :mrgreen: */

import java.util.*;
import javax.swing.JOptionPane;
public class Project{
public static void main(String []args){
String hula="";
String again="";
int ulit;
int hinula;
do{
Random rand = new Random();
int num = rand.nextInt(100);
do{
hula = JOptionPane.showInputDialog("Please enter your hula " );
hinula = Integer.parseInt(hula);
if (hinula>num)
JOptionPane.showMessageDialog(null, "Lower!");
else if (hinula < num)
JOptionPane.showMessageDialog(null, "Higher!");
}while (hinula !=num);
JOptionPane.showMessageDialog(null, " Congrats! ");
again = JOptionPane.showInputDialog(" Do you want to play again?Press 1 for Yes and any number for No");
ulit = Integer.parseInt(again);
}while (ulit==1);
System.exit(0);
}
}

Member Avatar for Dukane

Add a counter variable that gets incremented everytime the loop which pops up the "guess" dialog runs.

I have modified your code by simply adding 3 lines to do jus that.

import java.util.*;
import javax.swing.JOptionPane;

public class Project {
    public static void main(String []args) {
        String hula="";
        String again="";

        int ulit;
        int hinula;

        do {
            Random rand = new Random();
            int num = rand.nextInt(100);
            int cycles = 0;

            do {
                cycles++;
                hula = JOptionPane.showInputDialog("Please enter your hula " );
                hinula = Integer.parseInt(hula);

                if (hinula>num)
                    JOptionPane.showMessageDialog(null, "Lower!");
                else if (hinula < num)
                    JOptionPane.showMessageDialog(null, "Higher!");
            } while (hinula !=num);
            JOptionPane.showMessageDialog(null, "Congrats! It took you " + cycles + " tries to win!");
            again = JOptionPane.showInputDialog("Do you want to play again?Press 1 for Yes and any number for No");
            ulit = Integer.parseInt(again);
        } while (ulit==1);
    System.exit(0);
    }
}

Check it out, compile it and see how it works. If not what you wanted, at least you'll have the idea now to fix it so it works how you want it to work!
Please use code tags when you post code on the forum!

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.