0

/*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);
}
}

2
Contributors
1
Reply
2
Views
10 Years
Discussion Span
Last Post by Dukane
0

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!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.