我是编码的新手,我一直在编写此代码并尝试使其正常工作,但每次运行它都会崩溃。我已经查了一下并编写了这段代码我已经关注了如何正确记下代码以及本网站的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;
}
}
主要方法似乎很好,因为它将isConsecutive方法中的所有内容都作为注释,除了&#39; return true;&#39;事实上,程序运行并打印出来。所以我知道问题出在第二种方法中。
如果有任何我没做的事请告诉我,我将不胜感激。毕竟我还在学习。
由于
答案 0 :(得分:0)
你对arrayCharToInt[x+1]
的所有调用都将在他们所处的循环的最后一次迭代中超出范围(例如,如果arrayCharToInt.length
等于5,则{{1}的最高值将要去的是4.然后x
等于5,这超出了具有五个单元格的数组的范围。您需要进行某种x+1
检查。
答案 1 :(得分:0)
在for循环中的方法isConsecutive:for (int x=0;x<arrayCharToInt.length ;x++ )
,你使用了arrayCharToInt[x+1]
如果arrayCharToInt lenth为4,那么您有arrayCharToInt [0]
到arrayCharToInt [3]
。
现在考虑一下这句话:arrayCharToInt[x+1]
当x为3时,此语句将评估为arrayCharToInt[4]
,导致数组索引超出范围异常
答案 2 :(得分:0)
在Array调用函数中出现问题时抛出此错误。 您得到了长度并将其打印出来。 例如:
int a[] = {1,2,3,4}
此数组的长度是
int length = a.length
所以length = 4
,但最高索引是 3 ,而不是4。这意味着数组的索引以0开头。因此,您必须打印:
arr[length-1];
在您的程序中,
x == arrayCharToInt.length - 1