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.