我的方法javaKeyword每次都打印false,尽管我输入了hello和gty。有没有办法解决这个问题?
一旦找到匹配项,就需要中断循环,否则后续的不匹配项将用false覆盖b。或者,更好的是,只要找到匹配的就回来。此外,关键字区分大小写,因此不应使用不区分大小写的比较:我的方法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
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