輸入10個整數,統計並輸出其中正數、負數和零的個數.

輸入10個整數,統計並輸出其中正數、負數和零的個數.

如果覺得好,#include void main(){int a[10],i;int positive_num,negative_num,zero_num;positive_num = negative_num = zero_num = 0;for(i=0;i 0)positive_num ++;else if(a[i] < 0)negative_num ++;else zero_…

編寫java程式,輸入個數不定的整數,輸入0時結束.統計這些整數中正數和負數的個數,並計算它們的總和.

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函數中調用即可