Java の初心者です。必要な回答が得られたにもかかわらず、例外がある理由を知りたいです。以下はソースコードです
package habeeb;
import java.util.*;
public class Habeeb
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int[] num = new int[30];
int i, count = 0;
System.out.println("Enter the integers between 1 and 100");
for (i = 1; i <= num.length; i++)
{
num[i] = input.nextInt();
if (num[i] == 0)
break;
count++;
}
Sorting(num, i, count);
}
public static void Sorting(int[] sort, int a, int con)
{
int j, count = 0;
for (j = 1; j <= con; j++)
{
if (sort[a] == sort[j])
count++;
}
System.out.println(sort[a] + " occurs " + count + " times");
Sorting(sort, a - 1, con);
}
}
そして、ここに出力があります
run: Enter the integers between 1 and 100 1 2 3 2 2 4 5 3 6 1 0 0 occurs 0 times 1 occurs 2 times 6 occurs 1 times Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 3 occurs 2 times 5 occurs 1 times 4 occurs 1 times 2 occurs 3 times 2 occurs 3 times 3 occurs 2 times 2 occurs 3 times 1 occurs 2 times 0 occurs 0 times at habeeb.Habeeb.Sorting(Habeeb.java:18) at habeeb.Habeeb.Sorting(Habeeb.java:21) at habeeb.Habeeb.Sorting(Habeeb.java:21) at habeeb.Habeeb.Sorting(Habeeb.java:21) at habeeb.Habeeb.Sorting(Habeeb.java:21) at habeeb.Habeeb.Sorting(Habeeb.java:21) at habeeb.Habeeb.Sorting(Habeeb.java:21) at habeeb.Habeeb.Sorting(Habeeb.java:21) at habeeb.Habeeb.Sorting(Habeeb.java:21) at habeeb.Habeeb.Sorting(Habeeb.java:21) at habeeb.Habeeb.Sorting(Habeeb.java:21) at habeeb.Habeeb.Sorting(Habeeb.java:21) at habeeb.Habeeb.Sorting(Habeeb.java:21) at habeeb.Habeeb.main(Habeeb.java:14) Java Result: 1