2

我是编码新手,我一直在编写这段代码并试图让它工作,但每次运行它都会崩溃。我已经查阅了资料并将编写此代码我已关注 java 的网站,了解如何正确写下代码以及此站点。

无论如何,如果有人能向我解释为什么这不起作用,我将不胜感激,因为在我看来,逻辑是存在的,但我不明白它为什么会崩溃。

我的代码:

    import java.util.Scanner;
    import java.lang.String;
    import java.util.*;
    public class Question1
    {
      public static void main(String[] args)
          {
             Scanner keyboard= new Scanner(System.in);
             System.out.println("Enter either letters or numbers and I'll magically tell you if they are consecutive :D");
             String inputedString= keyboard.nextLine();
             boolean consecutiveOrNot=isConsecutive(inputedString);
             System.out.println("Drum rolls...... Is it consecutive: "+ consecutiveOrNot);  //Problem with this line?
          }


      public static boolean isConsecutive(String inputedString)
          {
            //Storing string's units into an array and converting to UpperCase if necessary
            //and storing string's numerical value into the variable 'arrayCharToInt'
              char[] charIntoArray= new char[inputedString.length()];
              int[] arrayCharToInt= new int[inputedString.length()];
              for (int i=0;i<inputedString.length();i++ )
                {
                   charIntoArray[i]=inputedString.charAt(i);
                    if (Character.isLetter(charIntoArray[i]) && Character.isLowerCase(charIntoArray[i]))
                     {
                        charIntoArray[i]= Character.toUpperCase(charIntoArray[i]);
                      }
                    arrayCharToInt[i]=(int) charIntoArray[i];
                }




           // The next if statements and the methods that they call are used to verify 
           //that the content of the initial string is either letters or numbers, but not both together
              boolean[] continuous= new boolean[arrayCharToInt.length];
              boolean[] testContNumbersDecreasing= new boolean[arrayCharToInt.length];
              boolean[] testContNumbersIncreasing= new boolean[arrayCharToInt.length];
              boolean[] testContLettersDecreasing= new boolean[arrayCharToInt.length];
              boolean[] testContLettersIncreasing= new boolean[arrayCharToInt.length];
              Arrays.fill(continuous, true);
               if (lowestValue(arrayCharToInt)>=65 && highestValue(arrayCharToInt)<= 90)
                {
                    for (int x=0;x<arrayCharToInt.length ;x++ ) 
                    {
                       testContLettersIncreasing[x]=((arrayCharToInt[x+1]-arrayCharToInt[x]== 1) || (arrayCharToInt[x+1]-arrayCharToInt[x]== -25));
                       testContLettersDecreasing[x]=((arrayCharToInt[x]-arrayCharToInt[x+1]== 1) || (arrayCharToInt[x]-arrayCharToInt[x+1]== -25));
                    }
                    return (Arrays.equals(continuous,testContLettersIncreasing) || Arrays.equals(continuous,testContLettersDecreasing));
                }

               else if ((lowestValue(arrayCharToInt) >= 48) && (highestValue(arrayCharToInt)<= 57))
                {
                    for (int x=0;x<arrayCharToInt.length ;x++ ) 
                    {
                       testContNumbersIncreasing[x]=((arrayCharToInt[x+1]-arrayCharToInt[x]== 1) || (arrayCharToInt[x+1]-arrayCharToInt[x]== -9));
                       testContNumbersDecreasing[x]=((arrayCharToInt[x]-arrayCharToInt[x+1]== 1) || (arrayCharToInt[x]-arrayCharToInt[x+1]== -9));
                    }
                    return (Arrays.equals(continuous,testContNumbersIncreasing) || Arrays.equals(continuous,testContNumbersDecreasing));

                }
              else
                {
                    return false;
                }

          }



      public static int lowestValue(int[] array)
          {
                int lowest=array[0];
                  for (int counter=0; counter< array.length; counter++)
                    {
                      if( lowest>array[counter])
                            lowest= array[counter];
                    }
                    return lowest;
          }

      public static int highestValue(int[] array)
          {
               int highest=array[0];
                for (int counter=0; counter< array.length; counter++)
                    {
                      if( highest<array[counter])
                        highest= array[counter];
                    }
                return highest;
          }

    }

main 方法似乎很好,因为它将所有内容都放在 isConsecutive 方法中作为注释,除了 'return true;' 确实,该程序运行并打印为 true。所以我知道问题出在第二种方法的某个地方。

如果有什么我做得不对的地方,请告诉我,我们将不胜感激。毕竟我还在学习。

谢谢

4

3 回答 3

1

您的所有调用arrayCharToInt[x+1]都将在它们所在循环的最后一次迭代中超出界限(例如,如果arrayCharToInt.length等于 5,则最高x将是 4。但x+1等于 5,超出范围具有五个单元格的数组的边界)。您需要进行某种if( x == arrayCharToInt.length - 1)检查。

于 2013-10-06T04:03:24.763 回答
0

在 for 循环内的方法 isConsecutive: for (int x=0;x<arrayCharToInt.length ;x++ )中,您使用过arrayCharToInt[x+1]

如果 arrayCharToInt 长度为 4 ,那么你arrayCharToInt [0]必须arrayCharToInt [3].

现在考虑以下语句:arrayCharToInt[x+1] 当 x 为 3 时,此语句将计算为arrayCharToInt[4]导致数组索引越界异常

于 2013-10-06T04:07:19.393 回答
0

当 Array 调用函数出现问题时,会引发此错误。你得到了长度并打印出来。例如:

int a[] = {1,2,3,4}

这个数组的长度是,

int length = a.length

所以length = 4但最高索引是3,而不是 4。这意味着数组的索引以 0 开头。所以你必须打印:

arr[length-1];

在你的程序中,

x == arrayCharToInt.length - 1
于 2019-07-10T17:59:01.067 回答