Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.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
我的方法javaKeyword每次都打印false,尽管我输入了hello和gty。有没有办法解决这个问题?_Java - Fatal编程技术网

我的方法javaKeyword每次都打印false,尽管我输入了hello和gty。有没有办法解决这个问题?

我的方法javaKeyword每次都打印false,尽管我输入了hello和gty。有没有办法解决这个问题?,java,Java,一旦找到匹配项,就需要中断循环,否则后续的不匹配项将用false覆盖b。或者,更好的是,只要找到匹配的就回来。此外,关键字区分大小写,因此不应使用不区分大小写的比较: import java.util.*; public class StudentUtilities { static boolean s; public static int howLong (String x) { return x.length(); } public st

一旦找到匹配项,就需要中断循环,否则后续的不匹配项将用false覆盖b。或者,更好的是,只要找到匹配的就回来。此外,关键字区分大小写,因此不应使用不区分大小写的比较:

import java.util.*;

public class StudentUtilities {
    static boolean s;

    public static int howLong (String x) {
        return x.length();
    }

    public static boolean isCharacter (String x) {
        return (x.length() > 1);  
    }


    public static boolean javaKeyword() {
        boolean b = false;
        String[] x = {"hello", "gty"};

        Scanner in = new Scanner(System.in);
        System.out.println("Type a word");
        String s = in.nextLine();

        for (int i = 0; i < x.length; i++)
            b = x[i].equalsIgnoreCase(s));

        return b;

    }


    public static void main(String[] args) 
    {
        System.out.println(javaKeyword());
    }

}
for(int i=0;i
既然找到“true”,就必须中断执行

for (int i = 0; i < x.length; i++)
    if (x[i].equals(s))
        return true;
for(int i=0;i
for循环没有被封闭,因此之后的immediate语句只会被执行,这使得它总是返回false

使用下面的一个

for (int i = 0; i < x.Length; i++)
            {

                if (x[i].Equals(s))
                {
                    b = true;
                    break;
                }
                else
                {
                    b = false;
                }
            }
for(int i=0;i
能否提供javaKeyword()方法的代码删除
否则返回false
;这将导致与数组中第一个候选项不匹配的有效关键字返回false,而该关键字应返回true。
for (int i = 0; i < x.length; i++){ // for loop start
           if(x[i].equalsIgnoreCase(s))
            return true;

} // for loop end