我强烈建议您继续学习Java Collections。
你可以修改你的程序
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;
public class arrayQuestion {
public static void main(String[] args) {
List userInputArray = new ArrayList();
Scanner input = new Scanner(System.in);
System.out.println("Enter 10 Numbers ");
int count = 0;
int x;
try {
do {
x = input.nextInt();
if (x != 0) {
System.out.println("Given Number is " + x);
userInputArray.add(x);
} else {
System.out
.println("Program will stop Getting input from USER");
break;
}
count++;
} while (x != 0 && count < 10);
System.out.println("Numbers from USER : " + userInputArray);
} catch (InputMismatchException iex) {
System.out.println("Sorry You have entered a invalid input");
} catch (Exception e) {
System.out.println("Something went wrong :-( ");
}
// Perform Anything you want to do from this Array List
}
}
我希望这能解决你的疑问......
超出此范围,如果用户输入任何字符或上述无效输入,则需要处理例外情况