【PAT】乙级1012 数字分类(JAVA版)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main{
	
	public static String f1(List number) {
		int sum=0;
		for(int i:number) {
			if(i%2==0) {
				sum+=i;
			}
		}
		if(sum==0) {
			return "N";
		}else {
			return String.valueOf(sum);
		}
	}
	
	public static String f2(List number) {
		if(number.size()==0) {
			return "N";
		}
		int sum=0;
		for(int i=0;i number) {
		if(number.size()==0) {
			return "N";
		}
		return String.valueOf(number.size());
	}
	
	public static String f4(List number) {
		if(number.size()==0) {
			return "N";
		}
		double sum=0;
		for(int i:number) {
			sum+=(double)i;
		}
		double avg=sum/number.size();
		java.text.DecimalFormat df = new java.text.DecimalFormat("#.0"); 
		return String.valueOf(df.format(avg));
	}
	
	public static String f5(List number) {
		if(number.size()==0) {
			return "N";
		}
		int max=number.get(0);
		for(int i:number) {
			if(i>max) {
				max=i;
			}
		}
		return String.valueOf(max);
	}
	
	public static void main(String[] args) throws IOException {
		List a1=new ArrayList();
		List a2=new ArrayList();
		List a3=new ArrayList();
		List a4=new ArrayList();
		List a5=new ArrayList();
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		String str=br.readLine();
		br.close();
		String[] stra=str.split(" ");
		for(int i=1;i

java保留小数点后位数

java.text.DecimalFormat df = new java.text.DecimalFormat("#.0");

double d = 3.14159;

System.out.println(df.format(d));

 

注意:

第四个函数中,最后需要有小数位数,所以不能用int,需要用double

否则会自动转换数字的类型

你可能感兴趣的:(【PAT】乙级1012 数字分类(JAVA版))