1007
C基础-计负均正
NEUQer1193945471 Sec128 MB
描述数据评测
题目描述
保留两位小数
输入描述
输出描述
样例输入
1 2 3 4 5 6 7 8 9 10
-1 -2 -3 -4 -5 -6 -7 -8 -9 -10
样例输出
10
5.50
import java.util.Scanner;
public class Main{
public static void main(String arge[]){
int i = 0;
double average = 0;
double sum = 0;
int countfs = 0;
double countzs = 0;
int[] arrays = new int[20];
Scanner sc = new Scanner(System.in);
while(i<1){
for(int j = 0;j<20;j++){
arrays[j] = sc.nextInt();
if(arrays[j] >0){
countzs++;
sum += arrays[j];
}else{
countfs++;
}
i++;
}
}
average = sum/countzs;
System.out.println(countfs);
System.out.println(String.format("%.2f",average));
}
}
解决问题:1
Scanner sc = new Scanner(System.in);
while(i<20){
for(int j = 0;j<20;j++){
arrays[j] = sc.nextInt(); //输入20个数 直接用不同的接收者连续接收20次
}
解决问题:2 输出结果保留两位小数的一种方法
System.out.println(String.format("%.2f",a)); //即可把a的值 转变为小数点后保留两位小数