public static void test(){
Scanner sc = new Scanner(System.in);
long num = 0,negative = 0,positive = 0,sum = 0;
List nums = new ArrayList();
do{
System.out.println("please enter a number :");
String s = sc.nextLine();
if (isNumber(s)) {
num = Long.parseLong(s);
if(num != 0){
nums.add(num);
continue;
}
break;
}
System.out.println("not number !");
break;
}while(true);
for (Long n :nums) {
if(n > 0){
positive ++;
}else{
negative ++;
}
sum += n;
}
System.out.println("the negative :" + negative);
System.out.println("the positive :" + positive);
System.out.println("the sum :" + sum);
}
public static boolean isNumber(String s){
try {
Long.parseLong(s);
return true;
} catch (Exception e) {
return false;
}
}
在 main 函数中调用即可
编写java程序,输入个数不定的整数,输入0时结束.统计这些整数中正数和负数的个数,并计算它们的总和.
编写java程序,输入个数不定的整数,输入0时结束.统计这些整数中正数和负数的个数,并计算它们的总和.
其他人气:937 ℃时间:2019-09-20 06:18:58
优质解答
我来回答
类似推荐
猜你喜欢
- 1二次函数y=a(x+k)²+k(a≠0)图像的顶点在?
- 2小华4年后的年龄与小丽4年前的年龄相等,
- 3we can buy some apples for our teacher.some apples提问
- 4有四个数,每次从中取出3个数相加,得到的四个和分别是22/24/27/20.这4个数各是什么?
- 5作文 为自己竖起大拇指
- 6She was often heard ( ) in English.A.sing B.singing C.to sing
- 7矿井里的升降机,由静止开始匀加速上升,经5s速度达到6m/s后,又以此速度匀速度上升10s,然后匀减速上升,又经10s停在井口.求矿井的深度.
- 8《游山西村》和《过故人庄》分别表达了作者怎样的感情
- 9设X属于(0,2)时,求函数Y=根号下X乘(8-3X)的最大值.
- 10把20克糖放入80水中,水占糖水的几分之几?