问题
I'm still relatively new at Java and am working on a program for class. I have everything coded up and it's working great, but I need to exit a method when I type the string "exit". My current code will be below. I must have 3 methods (includes main) and a "while" loop (or do-while, but my professor has barred us from using those) as per the assignment. It's a "magic eight ball" program and the while loop is set to continue asking questions until the user types "exit" but I can't seem to get my "while" loop to work correctly. If I type exit as the first string it'll exit like it's supposed to, but if I type any other string it'll keep running, even after I type exit, and basically becomes an infinite loop of questions/answers. Any help is appreciated.
A side note, those numbers in the switch statement are purely place holders for future answers, so ignore them.
import java.util.*;
public class FirstProblem {
public static void main (String [] args) {
getQuestion();
}
public static String getQuestion () {
Scanner input = new Scanner (System.in);
System.out.print ("Ask any question you need answered and prepare yourself for the sage advice of......your computer.\nJust type \"exit\" when you can't handle the truth any longer.\nQuestion: ");
String question = input.nextLine();
String exit = "exit";
showAnswer();
while (!question.equals(exit)) {
getQuestion();
}
return question;
}
public static void showAnswer () {
int answer = (int)(Math.random() * 10);
switch (answer) {
case 0: System.out.println ("1"); break;
case 1: System.out.println ("2"); break;
case 2: System.out.println ("3"); break;
case 3: System.out.println ("4"); break;
case 4:System.out.println ("5"); break;
case 5:System.out.println ("6"); break;
case 6:System.out.println ("7"); break;
case 7: System.out.println ("8"); break;
case 8: System.out.println ("9"); break;
case 9: System.out.println ("10"); break;
}
return;
}
}
回答1:
Your problem is in this code:
showAnswer();
while (!question.equals(exit)) {
getQuestion();
}
This is doing a recursive call, if you don't know what recursion is read about it here at Wikipedia
The solution would be to change it to:
while (!question.equals(exit)) {
showAnswer();
System.out.print("Question: ");
question = input.nextLine();
}
Here's the code snippet on Ideone.
回答2:
Change the call inside the while loop from:
while (!question.equals(exit)) {
getQuestion();
}
to:
while (!question.equals(exit)) {
return getQuestion(); // <-- return here
}
Explanation:
Since that is a recursive call, when you finally type "exit" this answer exists only in the scope of the last call, this call returns to the previous call in which the question was different than "exit" and hence it displays the question again and again...
Reply to the comment about getting rid of the last answer:
If you don't want to display the answer after the user typed "exit" you can modify showAnswer()
to do so by passing it the question, and if it equals to "exit" bailout before doing the printing:
public static String getQuestion () {
...
showAnswer(question);
...
}
public static void showAnswer (String question) {
if ("exit".equals(question)) return;
...
}
来源:https://stackoverflow.com/questions/33195203/exiting-a-method-with-a-specific-string