能用Java帮我写一个背单词的app吗
时间: 2023-05-28 22:04:02 浏览: 183
当然可以。以下是一个简单的背单词的app的Java代码示例:
```
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Random;
public class VocabularyApp {
private ArrayList<String> words;
private ArrayList<String> meanings;
public VocabularyApp() {
words = new ArrayList<String>();
meanings = new ArrayList<String>();
}
public void addWord(String word, String meaning) {
words.add(word);
meanings.add(meaning);
}
public void startQuiz() {
Scanner input = new Scanner(System.in);
Random random = new Random();
int score = 0;
int total = words.size();
for (int i = 0; i < total; i++) {
int index = random.nextInt(words.size());
String word = words.get(index);
String meaning = meanings.get(index);
System.out.println("What is the meaning of " + word + "?");
String answer = input.nextLine();
if (answer.equalsIgnoreCase(meaning)) {
System.out.println("Correct!");
score++;
} else {
System.out.println("Wrong! The correct answer is " + meaning);
}
words.remove(index);
meanings.remove(index);
}
System.out.println("Quiz finished. Your score is " + score + "/" + total);
}
public static void main(String[] args) {
VocabularyApp app = new VocabularyApp();
app.addWord("apple", "a fruit");
app.addWord("book", "a written work");
app.addWord("computer", "an electronic device for processing data");
app.startQuiz();
}
}
```
这个app包含两个ArrayList,一个用于存储单词,另一个用于存储它们的意思。用户可以使用`addWord`方法向这两个列表中添加单词和它们的意思。`startQuiz`方法随机从这些列表中选择一个单词,并要求用户输入它的意思。如果用户回答正确,它会增加得分,否则会显示正确答案。在每次迭代后,单词和它的意思将从列表中删除,以确保不会重复出现。在main方法中,我们向这个app中添加了三个单词和它们的意思,然后启动了一个背单词的测验。
请注意,这只是一个非常简单的实现,您可以根据自己的需求进行修改和扩展。
阅读全文