菜鸟询问关于JAVA的问题!

来源:百度知道 编辑:UC知道 时间:2024/05/10 19:47:34
我是个新手..最近老师发了个题目要我们编..绞尽脑汁始终卡在几个环节上..
1.当我需要用户输入一组数据并且储存这些数据时,是不是要ARRAY和其他循环一起用?但问题又来了.如果在我和用户都不知道要输入多少个数据时,我该如何赋值给这个ARRAY?如果没有赋值编译会说未初始化..(并且要当用户输入0时循环停止)
2.假如问题1解决了.那么比如说用户输入10个数据,有正整数和负整数,如何将正的加在一起,负的加在一起?我尝试使用IF ELSE来解决这个问题..但我不知道如何把它们加在一起,每次最后输出的只能是在前面ARRAY里用户输入的最后一个数字..
望各位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.
这是题目的一部分..也是我不懂的部分..4楼回答的第二个问题我看懂了..可是第一个问题关于String我们还没有学到..只有这个方法么?
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[inde

1.你知道String [] args是什么意思吗 ?这个就表示一个数组啊,至于数组的长度 你可以看这个 String [] a = new String[a.length]; 这样 数组的长度就可以根据你输入的数的实际个数定义数组的长度
2.for(int i = 0 ;i<a.length;i++){
if (a[i]>0){sum+=a[i];}//正数
else (suma+=a[i])//0和负数
}

不好意思!我看了5分钟,硬是没看明白你说的什么意思!

建议你把老师的题目贴上来

我也看不懂说啥。发一下你写的代码来。

用ArrayList来存放数字,可以自动增加大小,用add()方法就可以了。
再新建两个double的变量,来存放正负数的和就可以了。