我基本上是在玩 eclipse 来保留我在 Java 课程介绍中学到的东西。我即将参加高级编程,我有一个初学者的问题......
我在下面创建了这个小程序,因为我很难理解方法。现在我已经了解了一般概念,我有一个新问题。我在下面有一个 do while 语句,如果您输入简单加法问题的正确答案,它就会起作用。如果您输入错误的答案,它会要求您再次尝试该问题(如我所愿)。如果您再次输入错误的答案,程序将停止。我希望它提示用户输入答案,直到他们正确为止。我该怎么做呢?
import java.util.Scanner; //needed for input
public class PracticeMethod {
public static void main(String[] args) {
// Create a Scanner
System.out.println(");
System.out.println();
makesPerfect();
String anotherRun = "yes";
Scanner input = new Scanner(System.in);
}
static void makesPerfect() {
Scanner input = new Scanner(System.in);
String anotherRun;
do {
System.out.print(" Math");
int x = (int) (Math.random() * 10) + 1;
int y = (int) (Math.random() * 10) + 1;
System.out.print(" What is " + x + "+" + y + "? ");
double answer = 0.0;
answer = input.nextDouble();
if (answer == x + y) {
System.out.println(" Yes, you are correct.");
}
if (answer != x + y) {
System.out.print(" No, that is not correct. ");
System.out.println();
System.out.print(" Why dont you try again? ");
System.out.println();
System.out.println();
System.out.print(" What is " + x + "+" + y + "? ");
answer = input.nextDouble();
}
System.out.println();
System.out.print("Enter yes if you want to run again: ");
anotherRun = input.next();
input.nextLine(); // causes skipping issue to fix
System.out.print("\n\n\n");
} while (anotherRun.equalsIgnoreCase("yes"));
}
}