我正在用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;
    }
   }

最佳答案

  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;
  }


这不是你所追求的。


您不想在for循环中返回true。除非您发现未维护订单,否则不会。否则您会回来太早。另一方面,如果您发现订单实际上已取消,则返回false是可以的。
您不想检查char-otherchar == 1还是-1,因为这太严格了。您要查看> 0或

关于java - 我的代码不会返回任何东西!输出只是空白,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19341619/

10-11 19:39
查看更多