取整方法

  1. 向上取整-->ceil为天花板的意思,表示向上取整
  2. 向下取整-->floor为地板的意思,表示向下取整
  3. 四舍五入-->round
  4. 就近取整-->返回最接近参数的整数,如果有两个数同样接近,则返回偶数的那个(暂不清楚具体操作)
import java.util.Scanner;
public class ljljlj {
	public static void main(String[] args) {
        System.out.println("-----------------三种取整方法---------------");
		float a=12.29f;
		System.out.println("请输入取整方式1、向上取整2、向下取整3、四舍五入");
		Scanner sc=new Scanner(System.in);
		int num=sc.nextInt(); 
		//向上取整-->ceil为天花板的意思,表示向上取整
		//向下取整-->floor为地板的意思,表示向下取整
		//四舍五入-->round
		//就近取整-->返回最接近参数的整数,如果有两个数同样接近,则返回偶数的那个(暂不给出)
		switch(num) {
		case 1:
			System.out.println(Math.ceil(a));//13
			break;
		case 2:
			System.out.println(Math.floor(a));//12
			break;
		case 3:
			System.out.println(Math.round(a));//12
			break;
		}	
	}
}	

本次主要是为了复习取整方式和switch 语句,采写成这个样子。。。

如果有清楚第四种方式的人,,,,求教

日常鸡汤不积跬步无以至千里。。。

猜你喜欢

转载自blog.csdn.net/the_best_hacker/article/details/81181864
今日推荐