简单的程序,用于检查输入的字符串是否为关键字(指定关键字)。

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

Simple Program to check whether input String is a keyword or not (keywords are specified)

问题

这是我尝试执行的代码,但无论输入是什么,我都得到相同的结果,即“不是关键字”。

以下是关键字列表:{break, case, continue, default, defer, else, for, func, goto, if, map, range, return, struct, type, var}

如果我的输入字符串存在于关键字列表中,我想要打印出 "是关键字";否则打印 "不是关键字"

/*
 * 要更改许可证标题,请在项目属性中选择许可证标题。
 * 要更改模板文件,请选择工具 | 模板
 * 并在编辑器中打开模板文件。
 */
package word.is.key;

import java.util.*;
public class WordIsKey {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String s = "break, case, continue, default, defer, else, for, func, goto, if, map, range, return, struct, type, var";
        String[] keywords = s.split(",", 16);
        String n = input.nextLine();
        String res = n + "不是关键字";
        for (int i = 0; i < keywords.length; i++) {
            if (keywords[i].equals(n)) {
                res = n + "是关键字";
                break;
            }
        }
        System.out.println(res);
    }
}
英文:

This is the code I'm trying to execute but I get the same result no matter what the input is, i.e. "&lt;input string> is not a keyword".

Here is the list of keywords: {break, case, continue, default, defer, else, for, func, goto, if, map, range, return, struct, type, var}

I want to print "&lt;input string> is a keyword" if my input string is present in the list of keywords; else print "&lt;input string> is not a keyword".

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package word.is.key;

/**
 *
 * @author JagritSharma
 */
import java.util.*;
public class WordIsKey {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner input= new Scanner(System.in);
        String s= &quot;break, case, continue, default, defer, else, for, func, goto, if, map, range, return, struct, type, var&quot;;
        String[] keywords= s.split(&quot;,&quot;, 16);
        String n= input.nextLine();
        String res= n+&quot; is not a keyword&quot;;
        for(int i=0;i&lt;keywords.length;i++){
            if(keywords[i].equals(n)){
                res= n+&quot; is a keyword&quot;;
                break;
            }
        }
        System.out.println(res);
    }
    
}

答案1

得分: 3

问题在于当您在&quot;,&quot;上拆分&quot;break, case&quot;时,您将获得字符串&quot;break&quot;&quot; case&quot;。因此,您的代码仅适用于第一个关键字break,因为所有其他关键字都有额外的空格。
您需要更改为在&quot;, &quot;上进行拆分,或者选择另一种方法在将它们与您的输入进行比较之前去除额外的空格,例如keywords[i].trim().equals(n)

英文:

The problem is that when you split &quot;break, case&quot; on &quot;,&quot; you will get the strings &quot;break&quot; and &quot; case&quot;. Your code therefore only works for the first keyword break since all other keywords have the extra space.
You need to either split on &quot;, &quot; or choose another method to remove the extra space before comparing them with your input i.e. keywords[i].trim().equals(n).

答案2

得分: 1

你所需做的就是从输入的关键词字符串中移除空格,即将其改为"default,case,break",而不是"default, case, break"。

当你将字符串拆分成数组时,它会在逗号处进行拆分,并将前面的空格附加到下一个元素之前,导致你的"case"被解析为" case"。

英文:

All you need to do is remove spaces from your input string of keywords i.e. make it as "default,case,break" instead of "default, case, break".

When you are splitting the string into an array, it is splitting on commas and appending the preceding space before the next element to the element itself. Thus your "case" is being taken as " case".

答案3

得分: 1

代替将关键字列表的字符串表示转换为关键字数组,为什么不直接初始化关键字数组:

    String[] keywords = {
        "break", "case", "continue", "default",
        "defer", "else", "for", "func", "goto",
        "if", "map", "range", "return", "struct",
        "type", "var"};
英文:

Instead of trying to turn a string representation of a list of keywords into an array of keywords, why not just initialize the array of keywords directly:

    String[] keywords= {
        &quot;break&quot;, &quot;case&quot;, &quot;continue&quot;, &quot;default&quot;,
        &quot;defer&quot;, &quot;else&quot;, &quot;for&quot;, &quot;func&quot;, &quot;goto&quot;, 
        &quot;if&quot;, &quot;map&quot;, &quot;range&quot;, &quot;return&quot;, &quot;struct&quot;, 
        &quot;type&quot;, &quot;var&quot;};

答案4

得分: 1

除了上面的内容,只需使用以下代码部分:

javax.lang.model.SourceVersion.isName( keyWord );

这样,您将始终获得您正在使用的版本的最新关键字列表。

英文:

In addition to the above just use:

javax.lang.model.SourceVersion.isName( keyWord );

That way you will always have the latest list of keywords for the version you are using.

huangapple
  • 本文由 发表于 2020年10月20日 21:33:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/64446274.html
匿名

发表评论

匿名网友

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

确定