输入

5.66

输出

6

写出一个程序,接受一个浮点数值,输出该数值的近似整数值。如果小数点后数值大于等于5,向上取整;小于5,则向下取整。

 

 

 

import java.util.Scanner;
 
 
public class Main {
 
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNext()){
            String input = scanner.next();
            if(!input.contains("."))
                System.out.println(Integer.parseInt(input));
            else{
                int index = input.indexOf(".");// 定位小数点的位置
                if(input.charAt(index+1)>='5')// 考察小数点后的第一个数字
                    System.out.println(Integer.parseInt(input.substring(0,index))+1);
                else
                    System.out.println(Integer.parseInt(input.substring(0,index)));
            }
                 
        }      
        scanner.close();
    }
 
 
}

posted on 2015-12-22 22:20  小飞虫子  阅读(766)  评论(0编辑  收藏  举报