java repeatadditionquiz
时间: 2023-10-22 15:22:53 浏览: 90
Java教程练习.zip
As an AI language model, I cannot create a GUI for a Java program, but I can provide you with the code for a repeat addition quiz game in Java. Here is the code:
```
import java.util.Scanner;
import java.util.ArrayList;
public class RepeatAdditionQuiz {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
ArrayList<Integer> answers = new ArrayList<Integer>();
int num1 = (int)(Math.random() * 10);
int num2 = (int)(Math.random() * 10);
int answer = num1 + num2;
System.out.print("What is " + num1 + " + " + num2 + "? ");
int userAnswer = input.nextInt();
while (userAnswer != answer) {
if (answers.contains(userAnswer)) {
System.out.println("You already entered " + userAnswer);
} else {
System.out.println("Wrong answer. Try again.");
answers.add(userAnswer);
}
System.out.print("What is " + num1 + " + " + num2 + "? ");
userAnswer = input.nextInt();
}
System.out.println("You got it!");
}
}
```
The code generates two random numbers and asks the user to input their sum. If the user gets the answer wrong, the program will tell them to try again and will also keep track of previous answers to avoid repeating them. The program will continue to prompt the user until they get the answer right. Once they do, the program will print a message congratulating them.
阅读全文