import java.io.*;
import java.util.Scanner;
public class ws1qn2
{
public static void main(String[] args) throws IOException
{
Scanner input=new Scanner(System.in);
int a;
int d;
System.out.println("Please enter the number of characters the word has: ");
d=input.nextInt();
a=d-1;
char word[]=new char[a];
for (int b=0;b!=a;b++)
{
System.out.println("Please enter character no."+b+1);
String str;
str=input.next();
char c=str.charAt(b);
word[a-b]=c;
}
for (char reverse : word)
{
System.out.print(reverse);
}
}
}
这是我运行程序时发生的情况:
Please enter the number of characters the word has:
3
Please enter character no.01
s
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at ws1qn2.main(ws1qn2.java:22)
Process completed.
帮助?它看起来像堆栈溢出,但我不知道如何修复它。