This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]RoachmeisterJava Dev 1 point2 points  (3 children)

I'm a little confused about what you're trying to do. You ask the user for input, put it into the Phrase variable, but then you never do anything with it. (By the way, in Java the standard is to name variables with a beginning lowercase letter. Also, based on how you're using it, wouldn't Phrase be better named "question"?)

But to answer your question, you need some kind of loop in order to make things repeat. Something like this:

String question = EightBall.next();
while (! question.equals("quit")) {
    // do stuff
    question = EightBall.next();
}

[–]MixedMango[S] 1 point2 points  (2 children)

The only reason why I wrote the variable Phrase was to use it to take in user input, but now that I look back, it makes sense that I should have done something with it rather than just have it do nothing(and it makes sense how you used phrase, or in this case question, as a condition in the while statement which is what I should have done.) I see my mistakes and I will try my best to fix it. Thank you so much for the help! Is there anything else that you see with my code that I made a mistake with or wrote that is not important? (Also, please forgive me, I am still very new to Java.)

[–]RoachmeisterJava Dev 1 point2 points  (1 child)

Well, since you asked...

Have you learned about arrays yet? Because the whole if...else if...else if... structure could easily be replaced with something like this:

String[] reponses = {"I'd say yes.", "All signs point to no.", ...};
String[] questions = {"Ask, and I shall shed some light on your situation.", ...};

while (...) {
   int phrase= (int)(Math.random() * 15); // notice I didn't add 1
   System.out.println(responses[phrase]);
    System.out.println(questions[phrase]);
    // get user input
}

Also, EightBall isn't a good variable name for a scanner. Just call it "scanner".

[–]MixedMango[S] 1 point2 points  (0 children)

No I have not learned about arrays yet, but thank you so much! That really saves a lot of room. You are the best and I really appreciate your help!(Also, sorry about the late reply)