新手关于JAVA问题!

以下是我要做的一个程序中的一小部分:
The loyal subject is repeatedly prompted to enter either an amount of income (a positive value) or an amount of deduction (a negative value). This continues until a zero value is entered. The positive values are summed to form the income, and the negative values are summed to form the deduction.
其中我对如何实现当用户输入0时自动结束有疑问,我知道用while循环可以实现,但使用while循环时还能实现记下ARRAY中每个需要储存的数字吗?

以下是我写的一部分代码,我用的办法是让用户自己输入需要储存多少个amount。但这个貌似不符合我们老师给的题目。题目要求是当用户输入0时这个循环自动结束,可现在的情况是我要用户告诉我他要输入多少个数字后,在输入完这些数字后自动停止。请各位JAVA大大为我指点迷津!
System.out.print("Please enter how many values to store:");
numberOfAmount = keyboard.nextInt();
amount = new int[numberOfAmount + 1];
for (index = 0; index < amount.length; index++) {
System.out.print("Please enter the amount :");
amount[index] = keyboard.nextInt();
System.out.println("If you are finished, please enter zero!");
if (amount[index] < 0) {
deduction += Math.abs(amount[index]);
} else {
income += amount[index];
}
1楼的..我知道BREAK可以啊..但打个比方..USER要输入10个值.他需要在"Please enter how many values to store" 后输入10对吧?我意思是有什么办法可以省略这一步,也就是说不需要USER自己输入,当他输入10个数字以后输入0,然后自动结束..当然我也不知道USER会要输入多少个,所以我也不能定义amount = new int[]中括号里是多少.其实意思就是要实现:数组的长度就要根据用户输入的数的实际个数定义数组的长度

看不明白什么意思 你直接获取到输入的值 然后if(值==0)break;不就行了吗
温馨提示:答案为网友推荐,仅供参考
相似回答