Compare input with List<String> , tell the user if input is correct, almost correct or wrong

huangapple go评论77阅读模式
英文:

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(&quot;** GLOS&#214;VNING-ENGELSKA **&quot;);
            System.out.println(&quot;Write the English word for the given Swedish. Quit by pressing Q &quot;);
         //   System.out.println(&quot;the dictionary is : &quot; + getDictionary()); // to see that the dictionary is correct
        }

        private static Map&lt;String, List&lt;String&gt;&gt; getDictionary() {

            Map&lt;String, List&lt;String&gt;&gt; map = new HashMap&lt;&gt;();
            String[][] dictionary = {
                    // Swedish, Translation1, Translation2, ...
                    {&quot;bil&quot;, &quot;car&quot;},
                    {&quot;sn&#228;ll&quot;, &quot;kind&quot;},
                    {&quot;k&#246;r&quot;, &quot;drives&quot;, &quot;choir&quot;},
                    {&quot;hej&quot;, &quot;hello&quot;},
                    {&quot;&#228;pple&quot;, &quot;apple&quot;},
                    {&quot;kort&quot;, &quot;short&quot;}

            };
            for (String[] entry : dictionary) {
                String sweWord = entry[0]; // E.g. &quot;k&#246;r&quot;
                List&lt;String&gt; translations = new ArrayList&lt;&gt;();
                for (int i = 1; i &lt; entry.length; i++) {    // skips the first (swedish) word
                    translations.add(entry[i]); // e.g. &quot;drives&quot;

                }
                map.put(sweWord, translations);
                Querywords(map, sweWord, translations);
            }

            return map;
        }
// The swedish words asked
        private static List&lt;String&gt; Querywords(Map&lt;String, List&lt;String&gt;&gt; map, String sweWord, List&lt;String&gt; translations) {
            System.out.println(sweWord + &quot;:&quot;);
            List&lt;String&gt; translation = Translation(sweWord, translations, map);
            return translation;
        }
// The correct answer/answers.
        private static List&lt;String&gt; Translation(String sweWord, List&lt;String&gt; 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 &lt; translation.size(); i++)  {
                boolean correct = translation.get(i).equalsIgnoreCase(inputString);
                if (correct == true) {
                    correctans++;
                    System.out.println(translation.get(i));
                 //   System.out.println(translation+&quot;translation&quot;+i); for me to se that the code knows the right answer(s)..
                    System.out.println(&quot;Correct! &quot; + correctans + &quot;of &quot; + i + &quot; questions.&quot;);
                } else {

                    System.out.println(&quot;wrong&quot;); //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(&quot;** GLOS&#214;VNING-ENGELSKA **&quot;);
System.out.println(&quot;Write the Swedish word for the given English. Quit by pressing Q &quot;);
Map&lt;String, List&lt;String&gt;&gt; map = new HashMap&lt;String, List&lt;String&gt;&gt;();
List&lt;String&gt; setone = new ArrayList&lt;String&gt;();
setone.add(&quot;bil&quot;);
setone.add(&quot;vagn&quot;);
setone.add(&quot;godsfinka&quot;);
List&lt;String&gt; settwo = new ArrayList&lt;String&gt;();
settwo.add(&quot;slag&quot;);
settwo.add(&quot;snall&quot;);
List&lt;String&gt; setthree = new ArrayList&lt;String&gt;();
setthree.add(&quot;halla&quot;);
setthree.add(&quot;Hoy&quot;);
map.put(&quot;car&quot;, setone);
map.put(&quot;kind&quot;, settwo);
map.put(&quot;hello&quot;, setthree);
for (Map.Entry&lt;String, List&lt;String&gt;&gt; entry : map.entrySet()) {
String key = entry.getKey();
System.out.println(key +&quot;:&quot;);
Scanner scanner = new Scanner(System.in);
String inputString = scanner.nextLine().trim();
if (entry.getValue().contains(inputString)){
correct++;
System.out.println(&quot;Correct!&quot; + correct + &quot;of&quot; + map.size());
} else {
System.out.println(&quot;Wrong!&quot; + correct + &quot;of&quot; + map.size());
}
}
}
}

huangapple
  • 本文由 发表于 2020年8月3日 22:54:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63231798.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定