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.


>> REGISTER NOW TO START POSTING


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

Results 1 to 3 of 3

Thread: Magic 8 Ball Program please help with loops!

  1. #1
    Junior Member
    Join Date
    Nov 2018
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Magic 8 Ball Program please help with loops!

    Hello, I am attempting to make a program that simulates a magic 8 ball. You ask questions and it will respond accordingly, for the most part I have most of the program written and it all compiles, however, I am trying to get the program to loop and ask if the user wants to go again but it does seem to want to. I am having a bit of trouble with loops can anyone help show me how to approach this problem? I apologize if my program is all over the place I am still new.


     
    import javax.swing.JOptionPane;
    import java.util.Random; 
    import java.util.Scanner;
     
    public class Magic8Ball
    {
       public static void main(String[] args)
       {  
     
             Scanner input = new Scanner(System.in);
     
             String userInput = JOptionPane.showInputDialog("What is your question?");
     
             String answer = Magic8Ball();
             JOptionPane.showMessageDialog(null, answer);
     
             JOptionPane.showInputDialog("Would you like to try again? Y or N");
             String again = input.nextLine();
                if(again.equals("Y") || again.equals("y"))
                {
                   do{
                      JOptionPane.showInputDialog("What is you question?");
     
                      JOptionPane.showMessageDialog(null, answer);
     
                      JOptionPane.showInputDialog("Would you like to try again? Y or N");
                      String again2 = input.nextLine();
                   }while(again.equals("Y") || again.equals("y"));
                } else{
                JOptionPane.showMessageDialog(null, "Have a nice day.");
             } 
          }
     
     
       public static String Magic8Ball()
       {
          Random rand = new Random();
     
          String[] responses = {
             "it is certain.",
             "It is decidedly so.",
             "Without a doubt.",
             "Yes - definitely.",
             "You may rely on it.",
             "As I see it, yes.",
             "Most likely.",
             "Outlook good.",
             "Yes.",
             "Signs point to yes.",
             "Reply hazy, try again.",
             "Ask again later.",
             "Better not tell you now.",
             "Cannot predict now.",
             "Concentrate and ask again.",
             "Don't count on it.",
             "My reply is no.",
             "My sources say no.",
             "Outlook not so good.", 
             "Definitely not.",
             "Very doubtful.",
          };
     
          return responses[new Random().nextInt(responses.length)];
     
       }
    }
    Last edited by Spootdude; November 2nd, 2018 at 02:13 AM.

  2. #2
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Magic 8 Ball Program please help with loops!

    I haven't run this but for now, try using again2 in your second answer check conditional. But you will need to declare it outside of your loop. Or just reuse again. And you may want to use the String method equalsIgnoreCase method instead of equals.

    Edit: OK, one major problem is that you're taking input from a scanner. The JOptionPane returns the input string for you.

    Regards,
    Jim
    Last edited by jim829; November 1st, 2018 at 04:15 PM.

  3. #3
    Junior Member
    Join Date
    Nov 2018
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Magic 8 Ball Program please help with loops!

    Thank you so much, Jim! I was able to go through and solve the little puzzle and your comment helped a ton! Thanks for teaching me the equalsIgnoreCase I really appreciate it!

Similar Threads

  1. Java Program - Graphics - Ball Drop and Retrieve
    By kbrady481 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: May 1st, 2013, 04:47 PM
  2. need help with this basic program containing loops
    By kingslayer in forum Loops & Control Statements
    Replies: 2
    Last Post: March 23rd, 2012, 06:54 PM
  3. Bouncing Ball Program Random Color Change
    By coderEvolution in forum What's Wrong With My Code?
    Replies: 10
    Last Post: March 3rd, 2011, 03:01 PM
  4. Program with for loops help
    By ixjaybeexi in forum Loops & Control Statements
    Replies: 23
    Last Post: October 8th, 2009, 10:05 AM