英文:
Compare input with List<String> , tell the user if input is correct, almost correct or wrong
问题
以下是您的代码的翻译部分:
我是Java的新手,所以如果这段代码让你眼花缭乱,我深感抱歉。
我要制作一个简单的“术语测试”,从瑞典语翻译成英语。
我使用哈希映射来存储词典。我遇到的问题是如何将用户输入与正确的翻译进行比较。
我创建了一个for循环,遍历翻译数组。在这个循环中,我想将用户的输入与数组中的字符串进行比较,并存储正确答案数(correctans)和问题数(I)。
我无法正确更新正确答案数(correctans)和问题数(I)。
此外,我难以找到一种有效的方法来比较输入与字符串以检查正确性。
请注意,我只翻译了您代码中的注释和文字部分,不包括代码本身。如果您需要进一步的帮助或有其他问题,请随时提出。
英文:
I am new to java, so I apologies if the code makes your eyes bleed.
I am to make a simple "glossary test" in Swedish to English.
I have used hashmap to store the dictionary. The problems I have is when to compare user input with the correct translations.
I have created a for loop that loops through the array of translations. In that loop I would like to check the users input with the strings in that array and store correctans (number of correct answers) and I (how many questions that have been asked).
I can't get the correctans and I to update correctly.
Also I have trouble finding a good way to compare input with the string to check correctness.
My code :
import java.util.*;
public class MyClass {
public static void main(String[] args) {
Mainmenu();
getDictionary();
}
private static void Mainmenu() {
System.out.println("** GLOSÖVNING-ENGELSKA **");
System.out.println("Write the English word for the given Swedish. Quit by pressing Q ");
// System.out.println("the dictionary is : " + getDictionary()); // to see that the dictionary is correct
}
private static Map<String, List<String>> getDictionary() {
Map<String, List<String>> map = new HashMap<>();
String[][] dictionary = {
// Swedish, Translation1, Translation2, ...
{"bil", "car"},
{"snäll", "kind"},
{"kör", "drives", "choir"},
{"hej", "hello"},
{"äpple", "apple"},
{"kort", "short"}
};
for (String[] entry : dictionary) {
String sweWord = entry[0]; // E.g. "kör"
List<String> translations = new ArrayList<>();
for (int i = 1; i < entry.length; i++) { // skips the first (swedish) word
translations.add(entry[i]); // e.g. "drives"
}
map.put(sweWord, translations);
Querywords(map, sweWord, translations);
}
return map;
}
// The swedish words asked
private static List<String> Querywords(Map<String, List<String>> map, String sweWord, List<String> translations) {
System.out.println(sweWord + ":");
List<String> translation = Translation(sweWord, translations, map);
return translation;
}
// The correct answer/answers.
private static List<String> Translation(String sweWord, List<String> translation, Map map) {
Scanner scanner = new Scanner(System.in);
String inputString = scanner.nextLine().trim();
int correctans=0;
// add: to quit when Q is pressed
for (int i=0; i < translation.size(); i++) {
boolean correct = translation.get(i).equalsIgnoreCase(inputString);
if (correct == true) {
correctans++;
System.out.println(translation.get(i));
// System.out.println(translation+"translation"+i); for me to se that the code knows the right answer(s)..
System.out.println("Correct! " + correctans + "of " + i + " questions.");
} else {
System.out.println("wrong"); //more code
}
}
return translation;
}
}
答案1
得分: 0
以下是翻译好的代码部分:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class Dict {
public static void main(String[] args) {
int correct=0;
System.out.println("** GLOSÖVNING-ENGELSKA **");
System.out.println("Write the Swedish word for the given English. Quit by pressing Q ");
Map<String, List<String>> map = new HashMap<String, List<String>>();
List<String> setone = new ArrayList<String>();
setone.add("bil");
setone.add("vagn");
setone.add("godsfinka");
List<String> settwo = new ArrayList<String>();
settwo.add("slag");
settwo.add("snall");
List<String> setthree = new ArrayList<String>();
setthree.add("halla");
setthree.add("Hoy");
map.put("car", setone);
map.put("kind", settwo);
map.put("hello", setthree);
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
String key = entry.getKey();
System.out.println(key + ":");
Scanner scanner = new Scanner(System.in);
String inputString = scanner.nextLine().trim();
if (entry.getValue().contains(inputString)){
correct++;
System.out.println("Correct! " + correct + " of " + map.size());
} else {
System.out.println("Wrong! " + correct + " of " + map.size());
}
}
}
}
请注意,上述翻译的代码部分并没有涵盖所有注释和输出内容。如果您有进一步的问题或翻译需求,请随时提问。
英文:
To get the number of questions, just use the size of map as you are allowing user to enter the answers for all your keys in the dictionaries and for your count issue, please find simple code snippet. you can use as below:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class Dict {
public static void main(String[] args) {
int correct=0;
System.out.println("** GLOSÖVNING-ENGELSKA **");
System.out.println("Write the Swedish word for the given English. Quit by pressing Q ");
Map<String, List<String>> map = new HashMap<String, List<String>>();
List<String> setone = new ArrayList<String>();
setone.add("bil");
setone.add("vagn");
setone.add("godsfinka");
List<String> settwo = new ArrayList<String>();
settwo.add("slag");
settwo.add("snall");
List<String> setthree = new ArrayList<String>();
setthree.add("halla");
setthree.add("Hoy");
map.put("car", setone);
map.put("kind", settwo);
map.put("hello", setthree);
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
String key = entry.getKey();
System.out.println(key +":");
Scanner scanner = new Scanner(System.in);
String inputString = scanner.nextLine().trim();
if (entry.getValue().contains(inputString)){
correct++;
System.out.println("Correct!" + correct + "of" + map.size());
} else {
System.out.println("Wrong!" + correct + "of" + map.size());
}
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论