简体   繁体   English

比较来自两个不同字符串的 Char 的问题

[英]Trouble with comparing Char from two different strings

The program is suppossed to be a glossary test with 10 different words.该程序应该是一个包含 10 个不同单词的词汇表测试。 My code is working except one thing, and that being a function allowing the user to get an output saying "almost correct, the correct answer is ..." if some of the char is correct.我的代码除了一件事之外都在工作,并且这是一个允许用户获得输出说“几乎正确,正确答案是......”的函数,如果某些字符是正确的。

For example;例如; Correct answer is house.正确答案是房子。

user writes: housq -> output = "Almost correct, the correct answer is..."

user writes hsasa -> output = "Wrong, the correct answer is..."

My plan is to put the "Almost correct" through int count, in a span between the values 1-3.我的计划是通过 int count 将“几乎正确”放在值 1-3 之间的范围内。

But with this loop: It only counts correctly spelled words, for example但是有了这个循环:它只计算拼写正确的单词,例如

Correct word is house.

input -> house -> count=5

input -> housa -> count=0.

I'm new to Java and I apologize to everyone for potential bad code.我是 Java 新手,对于潜在的错误代码,我向大家道歉。

     Scanner scanner = new Scanner(System.in);
    String inputString = "";
    String[] eng = {"car", "house", "run", "blue", "bake", "jump", "swim", "moon", "road", "kind"};
    String[] swe = {"bil", "hus", "springa", "blå", "baka", "hoppa", "simma", "måne", "väg", "snäll"};
    int result = 0;
    int word = 0;
    int count = 0;
    int low = 2;
    int high = 3;

    for (int i = 0; i < swe.length; i++) {
        System.out.println(swe[i]);
        inputString = scanner.nextLine().trim();

        if (inputString.equalsIgnoreCase(eng[i])) {
            word++;
            result++;
            System.out.println("Korrekt! " + result + " rätt av " + word + " ord.");

            for (int j = 0; j < inputString.length(); j++) {
                if (inputString.charAt(j) == eng[i].charAt(j)) {
                    ++count;

                }
            }
                      if(count >= low){
                    System.out.println("Nästan rätt, korrekt svar är " + eng[i]);
                    word++;
                }


                if(count > low && count < high){
                    System.out.println("Nästan rätt, korrekt svar är " + eng[i]);
                    word++;
                }
        if (count == 0) {
            System.out.println("fel, rätt svar är " + eng[i]);
            word++;
        }
        System.out.println(count);

        if (inputString.equalsIgnoreCase("Q")) {
            System.out.println("Du svarade på totalt " + word + " glosor och hade " + result + " rätt. Välkommen åter!");

            System.exit(0);

        }

        count = 0;
    }
}

}} }}

I tried with your this code fragment like this and i get the output correct.我像这样尝试了你的这个代码片段,我得到了正确的输出。 Here is what i have tried这是我尝试过的

String inputString = "housr";
        String eng = "house";
        int count = 0;
        for(int j=0; j < inputString.length(); j++) {
            if(inputString.charAt(j) == eng.charAt(j)){
            ++count;
           }}
        System.out.println(count);

I got output as 4我的输出为4

And what are you trying, please paste your logic and the data also你在尝试什么,请粘贴你的逻辑和数据

Problem is with your if condition问题在于你的 if 条件

if (inputString.equalsIgnoreCase(eng[i]))

it checks for equals condition, if both your input string and eng[i] are equal then only your code executes and in your case your input in housq and you never find that word in eng array it skips the for loop and count will be your initialized value i;e, 0它检查相等条件,如果您的输入字符串和 eng[i] 相等,那么只有您的代码执行,在您的情况下,您在housq 中的输入,您永远不会在 eng 数组中找到该单词,它会跳过 for 循环,count 将是您的初始化值即 0

Is there any specific reason you are using that if condition您是否有任何特定原因使用 if 条件

And at this line而在这一行

if (inputString.charAt(j) == eng[i].charAt(j))

If your input string size and eng[i] string size is not equal than you will get StringIndexOutOfBoundsException exception .如果您的输入字符串大小和 eng[i] 字符串大小不相等,您将收到StringIndexOutOfBoundsException异常。

With slight changes in your code i can make it work to get your desired output通过对您的代码稍作更改,我可以使其工作以获得所需的输出

Scanner scanner = new Scanner(System.in);
        String inputString = "";
        String[] eng = {"car", "house", "run", "blue", "bake", "jump", "swim", "moon", "road", "kind"};
        String[] swe = {"bil", "hus", "springa", "blå", "baka", "hoppa", "simma", "måne", "väg", "snäll"};
        int result = 0;
        int word = 0;
        int count = 0;
        int low = 2;
        int high = 3;

        for (int i = 0; i < swe.length; i++) {
            System.out.println(swe[i]);
            inputString = scanner.nextLine().trim();
            if (inputString.equalsIgnoreCase(eng[i])) {
                word++;
                result++;
                System.out.println("Korrekt! " + result + " rätt av " + word + " ord.");
            }


            if(inputString.length() == eng[i].length()){

                for (int j = 0; j < inputString.length(); j++) {
                    if (inputString.charAt(j) == eng[i].charAt(j)) {
                        ++count;

                    }
                }
            }
                          if(count >= low){
                        System.out.println("Nästan rätt, korrekt svar är " + eng[i]);
                        word++;
                    }


                    if(count > low && count < high){
                        System.out.println("Nästan rätt, korrekt svar är " + eng[i]);
                        word++;
                    }
            if (count == 0) {
                System.out.println("fel, rätt svar är " + eng[i]);
                word++;
            }
            System.out.println(count);

            if (inputString.equalsIgnoreCase("Q")) {
                System.out.println("Du svarade på totalt " + word + " glosor och hade " + result + " rätt. Välkommen åter!");

                System.exit(0);

            }

            count = 0;
        }
    }

Here my input is housq这里我的输入是housq

Output i got is我得到的输出是

fel, rätt svar är car
0
Nästan rätt, korrekt svar är house
4
fel, rätt svar är run
0
fel, rätt svar är blue
0
fel, rätt svar är bake
0
fel, rätt svar är jump
0
fel, rätt svar är swim
0
fel, rätt svar är moon
0
fel, rätt svar är road
0
fel, rätt svar är kind
0
public static void spel() {
    Scanner scanner = new Scanner(System.in);
    String inputString = "";
    String[] eng = {"car", "house", "run", "blue", "bake", "jump", "swim", "moon", "road", "kind"};
    String[] swe = {"bil", "hus", "springa", "blå", "baka", "hoppa", "simma", "måne", "väg", "snäll"};
    int result = 0;
    int word = 0;
    int count = 0;
    int low = 2;
    int high = 3;

    for (int i = 0; i < swe.length; i++) {
        System.out.println(swe[i]);
        inputString = scanner.nextLine().trim();

        if(inputString.equalsIgnoreCase(eng[i])) {
            word++;
            result++;
            System.out.println("Korrekt! " + result + " rätt av " + word + " ord.");
        }

            for (int j = 0; j < inputString.length(); j++) {
                if (inputString.charAt(j) == eng[i].charAt(j)) {
                    ++count;

                }
            }
                      if(count >= low){
                    System.out.println("Nästan rätt, korrekt svar är " + eng[i]);
                    word++;
                }

        if (count == 0) {
            System.out.println("fel, rätt svar är " + eng[i]);
            word++;
        }
        System.out.println(count);

        if (inputString.equalsIgnoreCase("Q")) {
            System.out.println("Du svarade på totalt " + word + " glosor och hade " + result + " rätt. Välkommen åter!");

            System.exit(0);

        }

        count = 0;

}

}} }}

With this updated code.有了这个更新的代码。 I get the correct answer when most of the Char is correct.当大部分字符正确时,我得到正确答案。 For example housa = 4 -> "Almost correct...."例如 housa = 4 -> "几乎正确...."

But i still have the problem when difference in size in inputString and eng[i].length.但是当 inputString 和 eng[i].length 的大小不同时,我仍然遇到问题。 And also when the inputString is the correct answer both "Correct message" and "Almost" shows..而且当 inputString 是正确答案时,“正确消息”和“几乎”都显示..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM