题解 | #统计输入正数个数#

统计输入正数个数

http://www.nowcoder.com/practice/3266386e460a4e219d6b7d0bed2065b9



public class Main {
    
    public static void main(String[] args) {
        
        Scanner scanner = new Scanner(System.in);
        ArrayList<Integer> number= new ArrayList<>();
        //write your code here......
        int c=0;
        String s=scanner.nextLine();
        String[] s1= s.split(" ");
        int i=0;
        do
        {
            c=Integer.parseInt(s1[i]);
            if(c>0)
                number.add(c);
            i=i+1;
            
        }while (i<s1.length&&c>0);
        
        System.out.print(number.size());
    }
}

这个算法相对于直接while(scanner.nextInt()){count++}复杂多了,主要是忘了scanner输入的特性了,上述方法可以用于其他诸如文本的情况

全部评论

相关推荐

02-16 13:52
门头沟学院 Java
给🐭🐭个面试机会吧:嘿,mvbatis
点赞 评论 收藏
分享
03-21 08:46
已编辑
门头沟学院 C++
只写bug的程序媛:本科能找到好的,真不建议读研,提前占坑比较好,本科找不到好的,也不建议读研,因为两三年之后压力只会更大,唯一的解就是行业好起来
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务