华为31

package NiukeBrush;

import java.io.BufferedInputStream;
import java.util.Scanner;

//题目描述
//
//从输入任意个整型数,统计其中的负数个数并求所有非负数的平均值
//
//输入描述:
//输入任意个整数
//
//
//输出描述:
//输出负数个数以及所有非负数的平均值
//
//输入例子:
//-13
//-4
//-7
//
//输出例子:
//3
//0.0
//分析:1判断是否是负数 2归类 3进行累计 注意:他这里平均值是相对于正数
public class Huawei33 {

    public static void main(String[] args) {
        Scanner sc=new Scanner(new BufferedInputStream(System.in));
//      while(sc.hasNext())
//      {
//          //Question:如何输入任意整数
//          int a=sc.nextInt();
//          统计负数
            int temp=0;
           //统计正数
            int temp1=0;
            //统计平均数
//          double average=0.0;
            //定义总和
            int total=0;
        while(sc.hasNext())
        {
            int a=sc.nextInt();
            if(a<0)
            {
                temp++;
                continue;
            }
            else if(a==0)
            {
                continue;
            }
            else{
                temp1++;
                total +=a;
            }
        }
            System.out.println(temp);
            if(temp1==0){
            System.out.println(0.0f);
            }
            else{
                System.out.println(String.format("%.1f",total/(temp1+0.0f)));
            }
            sc.close();
        }

}
package NiukeBrush;

import java.util.Scanner;

public class Huawei34 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc=new Scanner(System.in);
        //定义正数
        int temp=0;
        //定义负数
        int temp1=0;
        //定义和
        double sum=0;
        while(sc.hasNext())
        {
            int a=sc.nextInt();
            if(a<0){
                temp1++;
                continue;
            }else{
                temp++;
                sum+=a;
            }
        }
        double average=sum/temp;
        System.out.println(temp1);
//      System.out.println(String.format("%.1f", average)); 
        System.out.println(Math.rint(average*10)/10);
        }

}

附上三种解法:感觉自己萌萌哒。
总结下:第一如果用到字符Scanner.hasNext()是可以连续输出的,
第二如果要用到精度到0.1,可以有两种解法:第一种String.format(“%.1f”,average))
第二种Math.rint(average*10)/10.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值