public class DivisionExample {
public static void main(String[] args) {
int a = 10;
int b = 3;
// 整数除法会自动取整,直接舍去小数部分
int result = a / b;
System.out.println("整数除法结果: " + result); // 输出 3
// 如果需要对浮点数进行除法并取整,可以使用 Math.floor 或 (int) 铸造
double c = 10.0;
double d = 3.0;
int floatResult = (int) (c / d);
System.out.println("浮点数除法并取整结果: " + floatResult); // 输出 3
// 使用 Math.floor 取整
int floorResult = (int) Math.floor(c / d);
System.out.println("使用 Math.floor 取整结果: " + floorResult); // 输出 3
}
}
10 / 3 的结果是 3。(int) 强制类型转换或使用 Math.floor() 方法将结果取整。例如 10.0 / 3.0 的结果为 3.333...,通过 (int) 转换后结果为 3,使用 Math.floor() 也会得到相同的结果。上一篇:java copy对象
下一篇:java builder用法
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站