4
\$\begingroup\$

I would just like some input on how I may improve my code(in terms of content not formatting) without changing the output. It's just a simple magic 8 ball program using dialog boxes.

Similar output

import java.security.SecureRandom;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class MagicEightBall {

    public static void main(String[] args) { 

        String answers[] = {"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", "Signs point to yes",
            "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", "Very doubtful"};

        //constructs a random number generator 
        SecureRandom randomNumber = new SecureRandom();
        ImageIcon image = new ImageIcon("8ball_small.jpg");
        int counter = 0;

        //while loop allows the user to continue asking questions until they click no
        while(counter != 1) {
            //prompts the user to enter a question 
            String answer = JOptionPane.showInputDialog(null, 
                    "PLease enter a yes or no question:", 
                    "WELCOME: Have your questions answered!", JOptionPane.INFORMATION_MESSAGE);

            //displays the answer
            if(answer != null)
                JOptionPane.showMessageDialog(null, answer+ "\n" + answers[randomNumber.nextInt(answers.length)], 
                "The Magic-8 Ball has responded.", JOptionPane.PLAIN_MESSAGE, image);

            //gives the user the option to click yes or no to continue asking questions
            counter = JOptionPane.showConfirmDialog(null, "", "Would you like to ask again?", 
                    JOptionPane.YES_NO_OPTION, 0, image);                           
        }

        //displays name after user enters no
        JOptionPane.showMessageDialog(null, "Programmed by my name", "Goodbye! Your answers have been answerd.", 
                JOptionPane.PLAIN_MESSAGE, image);
    }
}
\$\endgroup\$
1
\$\begingroup\$

Your program looks nice and is easy to understand. I only could find a single thing that you should change.

The variable counter is wrong, since it doesn't count anything. You should remove that variable completely. Instead, write while (true), and check the return value of the "again" dialog whether it is something else than JOptionPane.YES. If so, break out of the loop.

\$\endgroup\$
  • \$\begingroup\$ Personally I'd argue that while(true) is poor practise and would advise against it wherever possible. I don't think it's really necessary here... \$\endgroup\$ – Nathan Hoy Mar 22 '17 at 23:38
  • \$\begingroup\$ @NathanHoy In that case you should maybe write your own review stating that. From my experience, a do while loop might be a little better, but anything else probably leads to even larger code. I'm happy to be proven wrong, though. \$\endgroup\$ – Roland Illig Mar 23 '17 at 0:11
1
\$\begingroup\$

I did some refactoring... I disassembled everything into smaller pieces. Maybe the naming of the methods and vars could be better (I'm sure it can), but I wanted to show you, how to split 'stuff' into smaller pieces of code to make the main method (not meaning the main method in your code explicitly) tell the summary and move the details to separate methods.

public class MagicEightBall {

    private final static SecureRandom randomNumber = new SecureRandom();
    private final static String answers[] = {
            "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",
            "Signs point to yes",
            "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",
            "Very doubtful" };
    private final static ImageIcon image = new ImageIcon("8ball_small.jpg");

    public static void main(String[] args) {
        boolean askQuestion = true;

        while (askQuestion) {
            String question = getUserQuestion();
            String randomAnswer = getRandomAnswer();

            displayAnswer(question, randomAnswer);

            askQuestion = userWantsToAskAnotherQuestion();
        }

        showExitMessage();
    }

    private static String getUserQuestion() {
        return JOptionPane.showInputDialog(null,
                "PLease enter a yes or no question:",
                "WELCOME: Have your questions answered!",
                JOptionPane.INFORMATION_MESSAGE);
    }

    private static String getRandomAnswer() {
        return answers[randomNumber.nextInt(answers.length)];
    }

    private static void displayAnswer(String question, String randomAnswer) {
        JOptionPane.showMessageDialog(null, question + "\n" + randomAnswer, "The Magic-8 Ball has responded.", JOptionPane.PLAIN_MESSAGE, image);
    }

    private static boolean userWantsToAskAnotherQuestion() {
        return 0 == JOptionPane.showConfirmDialog(null, "", "Would you like to ask again?", JOptionPane.YES_NO_OPTION, 0, image);
    }

    private static void showExitMessage() {
        JOptionPane.showMessageDialog(null, "Programmed by my name", "Goodbye! Your answers have been answerd.", JOptionPane.PLAIN_MESSAGE, image);
    }
}
\$\endgroup\$
0
\$\begingroup\$

The only thing I would suggest is replacing the 1s and 0s with constants that have meaningful names (such as YES and NO).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.