用Java写一个答题系统
时间: 2024-01-01 21:04:27 浏览: 35
好的,下面是一个简单的答题系统的Java代码示例:
```java
import java.util.*;
public class QuizSystem {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int score = 0;
String answer;
// 创建问题和答案的哈希表
HashMap<String, String> quiz = new HashMap<String, String>();
quiz.put("What is the capital city of France?", "Paris");
quiz.put("What is the largest planet in the solar system?", "Jupiter");
quiz.put("What is the smallest country in the world?", "Vatican City");
// 遍历问题并进行答题
for (String question : quiz.keySet()) {
System.out.println(question);
answer = input.nextLine();
if (answer.equalsIgnoreCase(quiz.get(question))) {
System.out.println("Correct!");
score++;
} else {
System.out.println("Incorrect! The answer is " + quiz.get(question));
}
}
// 输出得分
System.out.println("You scored " + score + " out of " + quiz.size());
}
}
```
以上代码实现了一个简单的答题系统。它使用哈希表存储问题和答案,然后遍历问题并要求用户输入答案。如果答案正确,得分加1,否则输出正确答案。最后输出得分。