Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何正确记分_Java - Fatal编程技术网

Java 如何正确记分

Java 如何正确记分,java,Java,该程序被设计为在每次识别一个关键字时给一个点,但没有这样做。我相信解决办法很简单,但我还是不明白 我还需要这个显示时,没有关键字被检测到。最终,我希望能够根据重要性对每个关键字的值进行硬编码,而不是每个单词只有一个点 import java.util.*; public class KeyWords { public static void main(String[] args) { String who = null, what = null, where = nu

该程序被设计为在每次识别一个关键字时给一个点,但没有这样做。我相信解决办法很简单,但我还是不明白

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

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
import java.util.*;
公共类关键字{
公共静态void main(字符串[]args){
字符串who=null,what=null,where=null,when=null,
为什么=null,如何=null;
String[]关键字={“谁”、“什么”、“哪里”、“何时”、“为什么”、“如何”};
int RunningTotal=0;
扫描仪=新的扫描仪(System.in);
System.out.println(“请输入一个句子\n”);
字符串输入=scanner.nextLine().toLowerCase();
for(字符串关键字:关键字){
if(input.contains(关键字)){
System.out.println(“找到的关键字:+关键字”);
}
开关(关键字){
案例“世卫组织”:
RunningTotal++;
打破
案例“什么”:
RunningTotal++;
打破
案例“where”:
RunningTotal++;
打破
案例“何时”:
RunningTotal++;
打破
案例“为什么”:
RunningTotal++;
打破
案例“如何”:
RunningTotal++;//断开,始终添加所有6
打破
违约:
System.out.println(“未检测到关键字…”);//不起作用
}
}//for循环
系统输出打印项次(运行总数);

如果(RunningTotalSwitch语句将针对每个关键字执行,即使它不匹配。只需将Switch语句放入if中,它就会给出预期的结果。您是否尝试过eclipse和eclipse调试,这将使您的工作更容易自己发现这些问题。

Switch语句将针对每个关键字执行。)rd,即使它不匹配。只要将switch语句放在if中,它应该会给您预期的结果。您尝试过eclipse和eclipse调试,这将使您的工作更容易自己发现这些问题。

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


我建议您去掉switch语句。它是无用的。您可以使用循环中的if语句来执行同样的操作。我不会提供代码,但我认为这是一个巨大的提示。

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


我建议您去掉switch语句。它是无用的。您可以使用循环中的if语句来执行同样的操作。我不会提供代码,但我认为这是一个巨大的提示。

首先,不要执行类似的操作

String who = null, what = null, where = null, when = null,
                why = null, how = null;
这对Java开发人员来说太糟糕了,这种代码是由C等结构化语言重新定义的

我看到您正在获取
字符串输入
,并验证它是否包含数组中的
关键字
。您应该将其反转,如果在数组中找不到
关键字
,则应避免使用该方法。我已重写了您的代码,因此请查看这是否可以解决您的问题:

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
import java.util.*;
公共类关键字{
公共静态void main(字符串[]args){
列表关键字=数组.asList(“谁”、“什么”、“哪里”、“何时”、“为什么”、“如何”);
int RunningTotal=0;
扫描仪=新的扫描仪(System.in);
System.out.println(“请输入一个句子\n”);
字符串输入=scanner.nextLine().toLowerCase();
如果(!keywords.contains(input.trim().toLowerCase())){
System.out.println(“未检测到关键字…”);
}否则{
System.out.println(“找到的关键字:+输入”);
开关(输入){
案例“世卫组织”:
RunningTotal++;
打破
案例“什么”:
RunningTotal++;
打破
案例“where”:
RunningTotal++;
打破
案例“何时”:
RunningTotal++;
打破
案例“为什么”:
RunningTotal++;
打破
案例“如何”:
RunningTotal++;//断开,始终添加所有6
打破
}
}
系统输出打印项次(运行总数);

如果(RunningTotal首先,不要执行以下操作

String who = null, what = null, where = null, when = null,
                why = null, how = null;
这对Java开发人员来说太糟糕了,这种代码是由C等结构化语言重新定义的

我看到您正在获取
字符串输入
,并验证它是否包含数组中的
关键字
。您应该将其反转,如果在数组中找不到
关键字
,则应避免使用该方法。我已重写了您的代码,因此请查看这是否可以解决您的问题:

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
import java.util.*;
公共类关键字{
公共静态void main(字符串[]args){
列表关键字=数组.asList(“谁”、“什么”、“哪里”、“何时”、“为什么”、“如何”);
int RunningTotal=0;
扫描仪=新的扫描仪(System.in);
System.out.println(“请输入一个句子\n”);
字符串输入=scanner.nextLine().toLowerCase();
如果(!keywords.contains(input.trim().toLowerCase())){
System.out.println(“未检测到关键字…”);
}否则{
System.out.println(“找到的关键字:+输入”);
开关(输入){
案例“世卫组织”:
RunningTotal++;