题目描述:在主类main方法中输入若干个数据存储在数组中,Function类中编写方法计算这些数据中正数、负数和0的个数。在主类main方法中调用该方法输出结果。
输入格式:
第一行输入数组长度; 第二行依次输入数组元素;
输出格式:
与样例一致。
输入样例:
10
1 2 -3 4 -5 0 20 -4 13 0
输出样例:
正数:5
负数:3
零:2
代码实现:
import java.util.Scanner;
class Function{
int b1;
int b2;
int b3;
void func(int m[]){
int b1 = 0;
int b2 = 0;
int b3 = 0;
for(int i=0;i<m.length;i++){
if(m[i]==0){
b1++;
}
if(m[i]>0){
b2++;
}
if(m[i]<0){
b3++;
}
}
this.b1=b1;
this.b2=b2;
this.b3=b3;
}
}
public class Main{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int a[] = new int[n];
for(int i=0;i<n;i++){
a[i]= s.nextInt();
}
Function f=new Function();
f.func(a);
System.out.println("正数:"+f.b2);
System.out.println("负数:"+f.b3);
System.out.println("零:"+f.b1);
}
}