我不知道为什么当我运行它时,它设法通过第一部分,但是一旦它遍历数字并将它们全部打印出来。但一旦完成,它会显示java.lang.ArrayindexOutOfBoundsException
。为什么我会得到ArrayIndexOutOfBoundException?
package database;
import java.util.Scanner;
public class Run {
public static void main(String[] args) {
int counting =0;
int count = 0;
int totalAddUp =0;
int finalNumber = 0;
int searchingPlus = 0;
int caseSwitching = 0;
Scanner scan = new Scanner(System.in);
int userLength = 1000000000;
System.out.println("Please enter the number you wish to find exponentially: ");
int userInput = scan.nextInt();
System.out.println("Please enter the length in which you wish to find: ");
counting = scan.nextInt();
int[] array = new int[counting];
for(int i = 0; i < array.length + 1; i++) {
finalNumber = userInput + finalNumber;
array[totalAddUp] = finalNumber;
System.out.println(array[totalAddUp]);
totalAddUp++;
}
System.out.println("Press 1 to search for a certain number or press 2 to end the program");
switch(caseSwitching) {
case 1:
System.out.println("Please enter a number in which you would like to search");
int searching = scan.nextInt();
while(searchingPlus > userInput) {
if(userInput == array[searchingPlus]) {
System.out.println(array[searchingPlus]);
}
searchingPlus++;
}
break;
case 2:
System.out.println("Thanks have a great day");
break;
}
}
}
你能解释你的'案例1:'正在试图做什么? –
尝试用我
Nabin
> for(int i = 0; i