java 数组与用户输入_需要输入数组直到用户输入0 JAVA

我强烈建议您继续学习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

}

}

我希望这能解决你的疑问......

超出此范围,如果用户输入任何字符或上述无效输入,则需要处理例外情况

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值