如何在eclipse中随机输出20个题目
时间: 2024-05-12 21:15:22 浏览: 10
以下是在Eclipse中使用Java编程语言随机输出20个题目的示例代码:
```java
import java.util.Random;
public class RandomQuestions {
public static void main(String[] args) {
String[] questions = {"What is the capital of France?", "What is the square root of 144?", "Who discovered electricity?", "What is the largest planet in our solar system?", "What is the smallest country in the world?", "What is the atomic number of gold?", "What is the highest mountain in the world?", "What is the formula for water?", "What is the name of the first man to walk on the moon?", "What is the boiling point of water at sea level?", "What is the largest ocean on Earth?", "What is the currency of Japan?", "What is the chemical symbol for oxygen?", "What is the highest waterfall in the world?", "What is the name of the longest river in Africa?", "What is the name of the first computer programmer?", "What is the speed of light?", "What is the smallest particle in an element?", "What is the name of the largest desert in the world?", "What is the largest mammal in the world?"};
int count = 1;
Random rand = new Random();
while(count <= 20) {
int index = rand.nextInt(questions.length);
System.out.println(count + ". " + questions[index]);
count++;
}
}
}
```
该示例代码创建了一个字符串类型的数组,其中包含20个问题。然后,使用Java的Random类生成一个随机数来选择数组中的问题,并将其输出到控制台上。循环20次以输出20个问题。