我還沒有弄清楚數組的問題,你可能會看到。我試圖創建一個可以存儲用戶輸入的10個字符串的字符串,儘管我懷疑我已經走過了正確的道路。此刻我得到了錯誤'InputLoop.main(InputLoop.java:22)'java.lang.ArrayIndexOutOfBoundsException:10'任何幫助將不勝感激!數組存儲用戶輸入
import java.util.Scanner;
public class InputLoop
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
String array[] = new String[10];
int num =11, i =0;
System.out.println ("Enter a word");
for (i=0;i<num;i++)
{
while (scan.hasNextInt()) // while non-integers are present...
{
scan.next(); //...read and discard input, then prompt again
System.out.println ("Bad input. Enter a word");
}
array[i] = scan.next();
}
}
}