for (int i = 0; i <= (line.length()/2); i++) { \\loops from the first character until the character in the middle
if (line.charAt(i) != line.charAt(line.length() - i)) { \\checks if 1st character of line is not equal to the last character, and so on..
System.out.println("Entered string is not a palindrome.");
return; \\ends program, no need to check anything else
}
}
System.out.println("Entered string is a palindrome.");
我不断出现索引超出范围错误
索引超出范围错误正好说明了这一点。JavaString
的有效索引(也包括数组或集合)的范围是从0
到length() - 1
。i
从中减去时line.length()
,仍然可以得到line.length()
,如果i
是,则超出范围0
。这是一个错误的错误。
1
从第二个字符的索引中减去更多内容以保持界限。
if (line.charAt(i) != line.charAt(line.length() - i - 1)) {
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句