如何正确记分

时间:2018-01-17 16:18:03

标签: java

该程序旨在每次识别关键字时给出一个点,但不是这样做的。我确信修复很简单但是逃避了我。

我还需要在没有检测到关键字时显示。最终,我希望能够根据重要性对每个关键字进行硬编码。而不是每个单词只有一个点。

import java.util.*;

public class KeyWords {

    public static void main(String[] args) {
        String who = null, what = null, where = null, when = null,
                why = null, how = null;

        String[] keywords = {"who", "what", "where", "when", "why", "how"};

        int RunningTotal = 0;
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter a sentence\n");
        String input = scanner.nextLine().toLowerCase();

        for (String keyword : keywords) {
            if (input.contains(keyword)) {
                System.out.println("Found keyword: " + keyword);
            }

            switch (keyword) {
                case "who":
                    RunningTotal++;
                    break;
                case "what":
                    RunningTotal++;
                    break;
                case "where":
                    RunningTotal++;
                    break;
                case "when":
                    RunningTotal++;
                    break;
                case "why":
                    RunningTotal++;
                    break;
                case "how":
                    RunningTotal++;//Broken, always adds all 6
                    break;
                default:
                    System.out.println("No keywords detected...");//Does not work
            }
        }//for loop

        System.out.println(RunningTotal);
        if (RunningTotal <= 3) {
            System.out.println("Lack of communication skills");
        }
    }

}//class

5 个答案:

答案 0 :(得分:3)

如果您转到switch语句,您将看到您正在评估从关键字列表中提取的关键字。

我建议你摆脱switch语句。这没用。您可以使用循环内部的if语句来执行相同操作。我不会提供代码,但我认为这是一个巨大的提示。

答案 1 :(得分:2)

每个关键字都会执行Switch语句,即使它不匹配也是如此。只需将switch语句放在if中,它应该给你预期的结果。您是否尝试过eclipse和eclipse调试,这样可以让您自己轻松找到这些问题。

答案 2 :(得分:1)

首先,不要做像

这样的事情
String who = null, what = null, where = null, when = null,
                why = null, how = null;

这对Java开发人员来说太糟糕了。这种代码重新组合了C语言等结构化语言。

好吧,我看到你正在获取String input并验证它是否包含数组中的keyword。你应该反过来,如果你在数组中找不到keyword,你应该避免使用该方法。我已经重写了你的代码,所以看看这是否可以解决你的问题:

import java.util.*;

public class KeyWords {

    public static void main(String[] args) {
        List<String> keywords = Arrays.asList("who", "what", "where", "when", "why", "how");

        int RunningTotal = 0;
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter a sentence\n");
        String input = scanner.nextLine().toLowerCase();

        if (!keywords.contains(input.trim().toLowerCase())) {
            System.out.println("No keywords detected...");
        } else {
            System.out.println("Found keyword: " + input);

            switch (input) {
                case "who":
                    RunningTotal++;
                    break;
                case "what":
                    RunningTotal++;
                    break;
                case "where":
                    RunningTotal++;
                    break;
                case "when":
                    RunningTotal++;
                    break;
                case "why":
                    RunningTotal++;
                    break;
                case "how":
                    RunningTotal++;//Broken, always adds all 6
                    break;
            }
        }

        System.out.println(RunningTotal);
        if (RunningTotal <= 3) {
            System.out.println("Lack of communication skills");
        }
    }

}//class

答案 3 :(得分:0)

我认为你根本不需要那个switch语句。试试这个,让我知道它是否解决了你的问题:

public static void main(String[] args) {
    String[] keywords = {"who", "what", "where", "when", "why", "how"};

    int keywordsFound = 0;
    Scanner scanner = new Scanner(System.in);
    System.out.println("Please enter a sentence");
    String input = scanner.nextLine().toLowerCase();

    for (String keyword : keywords) {
        if (input.contains(keyword)) {
            keywordsFound++;
            System.out.println("Found keyword: " + keyword);
        }
    }//for loop

    System.out.printf("Found %d keywords\n", keywordsFound);
    if (keywordsFound <= 3) {
        System.out.println("Lack of communication skills");
    }
}

答案 4 :(得分:0)

谢谢大家, 我完全删除了switch语句并将所有内容放在for循环中。使用了以下代码:

public static void main(String[] args) {
    String[] keywords = {"who", "what", "where", "when", "why", "how"};

    int keywordsFound = 0;
    Scanner scanner = new Scanner(System.in);
    System.out.println("Please enter a sentence");
    String input = scanner.nextLine().toLowerCase();

    for (String keyword : keywords) {
        if (input.contains(keyword)) {
            keywordsFound++;
            System.out.println("Found keyword: " + keyword);
        }
    }//for loop

    System.out.printf("Found %d keywords\n", keywordsFound);
    if (keywordsFound <= 3) {
        System.out.println("Lack of communication skills");
    }
}