题解 | #统计数据正负个数#

统计数据正负个数

https://www.nowcoder.com/practice/3f33889582934a09b4e3ddd3cc976226

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 输入10个整数,分别统计输出正数、负数的个数。
        String nums;
        int[] arr = new int[10];
        nums = in.nextLine();
	   //split(" ")空格分隔字符串赋值给字符串数组
        String[] c_nums = nums.split(" ");
        for (int i = 0; i < 10; i++) {
		//将字符串数组转为int数组
            arr[i] = Integer.parseInt(c_nums[i]);
        }
        int z = 0;
        int f = 0;
        for (int i = 0; i < 10; i++) {
            if (arr[i] > 0) {
                z++;
            } else {
                f++;
            }
        }
        //positive:7
        //negative:3
        System.out.printf("positive:%d\n", z);
        System.out.printf("negative:%d", f);
    }
}

全部评论

相关推荐

牛客845590724号:单学校就够拷打了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务