为什么这个Java部门打印出零? [英] Why does this Java division print out zero?

查看:97
本文介绍了为什么这个Java部门打印出零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码行:

System.out.println(5/9);

我希望结果看到0.555,但它打印出零。有人能帮我理解为什么会这样吗?我目前正在学习编程并感谢他们的帮助。

I expect to see 0.555 as a result, but instead it prints out zero. Can someone help me understand why this happens? I am currently learning programming and appreciate the help.

谢谢!

推荐答案

这是因为你在不知不觉中做了什么是整数部门。
为了快速进行计算,当没有涉及十进制数时,计算机使用整数除法,因此十进制值丢失。

This happens because what you are unknowingly doing is Integer Division. To make calculations fast, computer uses Integer division method when there's no decimal number involved, and hence decimal values are lost.

试试这个:

System.out.println(5.0 / 9.0);

System.out.println(5.0 / 9);

System.out.println(5 / 9.0);

System.out.println((float) 5 / 9);

System.out.println(5 / (float) 9);

这篇关于为什么这个Java部门打印出零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆