【发布时间】:2013-10-13 03:40:21
【问题描述】:
我正在用 java 编写此代码来扫描数字或字母字符串以查看它们是否是连续的。一切似乎都运行良好,直到我尝试在其中放置一个布尔值以返回 true 或 false,但什么也没发生!我错过了什么?谢谢! :)
这里是:
public class Question1 {
public static void main(String[]args){
String s = "gFeD";
isConsecutive(s);
}
public static boolean isConsecutive(String s){
boolean letters;
letters = false;
int counter = 0;
String newS = s.toLowerCase();
for (int i = 0; i < newS.length() - 1; i++){
if (newS.charAt(i) - newS.charAt(i+1) == 1){
return true;
} else if (newS.charAt(i) - newS.charAt(i+1) == -1) {
return true;
}
}
return letters;
}
}
【问题讨论】:
-
"I'm writing this code in java to scan numerical or alphabetical strings to see if they are consecutive. Everything seems to be working fine until I try to place a boolean in there to return true or false but nothing is happening! What am I missing?"——也许是你的代码。您没有发布任何代码! -
如果您想将任何内容打印到控制台,请使用
System.out.println。如果您不使用它,请不要期望控制台中有任何内容 =\ -
请注意,您可以将
if条件简化为:if(Math.abs(newS.charAt(i) - newS.charAt(i+1)) == 1) return true;