列表

详情


JAVA10. 统计输入正数个数

描述

控制台输入整数,请设计一个死循环,当用户输入非正数时停止输入。请给出用户输入的正整数个数(默认输入个数不超过2147483647)

输入描述

若干整数,整数之间用空格隔开

输出描述

控制台输入的正整数个数

示例1

输入:

1 2 3 4 5 6 7 0

输出:

7

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Java 解法, 执行用时: 11ms, 内存消耗: 9368KB, 提交时间: 2022-04-24

import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;


public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] str = br.readLine().split(" ");
        int count;
        for(count = 0; count < str.length; count++){
            int num = Integer.parseInt(str[count]);
            if(num <= 0){break;}
        }
        System.out.println(count);
    }
}

Java 解法, 执行用时: 11ms, 内存消耗: 9472KB, 提交时间: 2022-07-06

import java.io.*;

public class Main {
    public static void main(String[] args)throws IOException {
        int count = 0;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] s = br.readLine().split(" ");
        boolean b =true;
        for(count=0;count<s.length;count++){
            int n = Integer.parseInt(s[count]);
            if(n<=0){break;}
        }
        System.out.print(count);

    }
}

Java 解法, 执行用时: 12ms, 内存消耗: 9388KB, 提交时间: 2022-05-10

import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException{
        int count = 0;
//         Scanner sc = new Scanner(System.in);

//         //write your code here......
//         int num = 0;
//         for(; sc.nextInt()>0; num++){
//             if(num >= 2147483647){
//                 break;
//             }
//         }
//         System.out.println(num);
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] s = br.readLine().split(" ");
        for(; count<s.length; count ++){
            if(Integer.parseInt(s[count]) <= 0){break;}
            
        }
        System.out.println(count);
        br.close();



    }
}

Java 解法, 执行用时: 12ms, 内存消耗: 9468KB, 提交时间: 2022-07-16

import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] str = br.readLine().split(" ");
        int count;
        for(count = 0; count < str.length; count++){
            int num = Integer.parseInt(str[count]);
            if(num <= 0){
                break;
            }
        }
        System.out.println(count);
    }
}

Java 解法, 执行用时: 12ms, 内存消耗: 9472KB, 提交时间: 2022-05-30

import java.io.StreamTokenizer;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;

public class Main{
    public static void main(String args[]) throws IOException{
        StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        
        int count = 0;
        while(true){
            st.nextToken();
            if(st.nval > 0.0) count++;
            else break;
        }
        System.out.println(count);
    }
}

上一题