Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: While JOptionPane equal Yes????

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Thanked 0 Times in 0 Posts

    Default While JOptionPane equal Yes????

    I want the while at the end to say while JOption says yes. Meaning when asked if they want to play again it will re loop and play it again if they click yes. I have looked and I can't find how to do it. I also used an "int" which was selection, but it says i need a boolean value which kinda makes sense but I dont know how..


    package lab3;
    // Import
    import java.util.Scanner;
    import javax.swing.JOptionPane;
    // Start
    public class Problem4 {
        public static void main (String[] args){
        // It's time to play the lottery!
    int selection;
         do{ int lottery = (int)(Math.random() * 1000);
          int runAgain;
        // Promt the user to enter a guess
        Scanner input = new Scanner(System.in);
        System.out.println("Enter your lottery pick (three digits): ");
        int guess = input.nextInt();
        // Get digits from lottery
        int lotteryDigit1 = lottery / 100;
        int lotteryDigit2 = lottery / 10;
        int lotteryDigit3 = lottery % 10;
        // Get digits from guess
        int guessDigit1 = guess / 100;
        int guessDigit2 = guess / 10;
        int guessDigit3 = guess % 10;
        System.out.println("The lottery number is " + lottery);
        // Check the guess
        if (guess == lottery)
            System.out.println("Exact match: You win $10,000!");
        else if (guessDigit2 == lotteryDigit1
              && guessDigit1 == lotteryDigit2)
            System.out.println("Match all digits: you win $3,000");
        else if (guessDigit1 == lotteryDigit1
              || guessDigit1 == lotteryDigit2
              || guessDigit1 == lotteryDigit3
              || guessDigit2 == lotteryDigit1
              || guessDigit2 == lotteryDigit2
              || guessDigit2 == lotteryDigit3
              || guessDigit3 == lotteryDigit1
              || guessDigit3 == lotteryDigit2
              || guessDigit3 == lotteryDigit3)
            System.out.println("Match one digit: You win $1,000");
            System.out.println("Sorry, no match. Try again!");
      selection = JOptionPane.showConfirmDialog(null,
     "Would you like to run another?","Confirmation",
    while (selection = Yes_Option);

  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Thanked 833 Times in 772 Posts
    Blog Entries

    Default Re: While JOptionPane equal Yes????

    The conditional needs a boolean, so you must use a boolean, or in this case compare the two integers using '=='. In addition, the API for JOptionPane explicitly states what is returned for this method based upon the user action.
    while (selection == JOptionPane.YES_OPTION);

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Thanked 0 Times in 0 Posts

    Default Re: While JOptionPane equal Yes????

    Ah, that makes sense.

    Thanks, I appreciate it!

Similar Threads

  1. Using Icons in JOptionPane
    By Jchang504 in forum AWT / Java Swing
    Replies: 3
    Last Post: September 19th, 2014, 02:28 PM
  2. Change to JOptionPane
    By Liuric in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 1st, 2010, 12:07 AM
  3. JOptionPane using If and Else
    By Liuric in forum Member Introductions
    Replies: 7
    Last Post: October 1st, 2010, 12:05 AM
  4. Nested Looping to find if Strings equal
    By aussiemcgr in forum Loops & Control Statements
    Replies: 4
    Last Post: July 9th, 2010, 03:08 PM
  5. Replies: 2
    Last Post: February 25th, 2010, 04:17 PM