赞
踩
好的,我可以为您提供一份简单的 Java 代码来实现一个背单词的小程序。
首先,我们需要准备一个单词列表和对应的中文释义。可以将这些信息存储在一个 HashMap 中,例如:
- Map<String, String> dictionary = new HashMap<>();
- dictionary.put("apple", "苹果");
- dictionary.put("banana", "香蕉");
- dictionary.put("orange", "橙子");
然后,我们可以使用一个循环来依次打印出每个单词,并等待用户输入对应的中文释义。如果输入正确,就提示用户答对了;如果输入错误,就提示用户答错了并显示正确的翻译。
这里是代码示例:
```java import java.util.Map; import java.util.Scanner;
public class Main { public static void main(String[] args) { // 创建一个字典 Map<String, String> dictionary = new HashMap<>(); dictionary.put("apple", "苹果"); dictionary.put("banana", "香蕉"); dictionary.put("orange", "橙子");
- // 创建一个 Scanner 用于读取用户输入
- Scanner scanner = new Scanner(System.in);
-
- // 遍历字典中的所有单词
- for (Map.Entry<String, String> entry : dictionary.entrySet()) {
- String word = entry.getKey();
- String translation = entry.getValue();
-
- // 打印单词并等待用户输入翻译
- System.out.print(word + ": ");
- String userInput = scanner.nextLine();
-
- // 判断用户输入是否正确
- if (userInput.equals(translation)) {
- System.out.println("正确!");
- } else {
- System

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。