在java语言中划分错误 [英] Dividing error in java language
本文介绍了在java语言中划分错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个简单的java程序,它的运行方式与我认为的不同。
I have a simple java program that does not operate the way that I think it should.
public class Divisor
{
public static void main(String[] args)
{
int answer = 5 / 2;
System.out.println(answer);
}
}
为什么不打印2.5?
推荐答案
5/2是整数除法(你甚至将它存储在整数变量中),如果你想要它是2.5,你需要使用浮点除法:
5 / 2 is integer division (you're even storing it in an integer variable), if you want it to be 2.5, you need to use floating point division:
double answer = 5.0 / 2.0;
整数除法总是等于正常的数学除法,向下舍入到最接近的整数。
Integer division is always going to be equal to normal mathematical division rounded down to the nearest integer.
这篇关于在java语言中划分错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文