Java Division错误 [英] Java Division error

查看:138
本文介绍了Java Division错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下变量:

int first = 0;
int end = 0;

在公共类中声明。

在方法中:

double diff = end / first;
double finaldiff = 1 - diff;

<$ c $上的结束变量c> System.out.println 是 527 第一个 480

The end variable on System.out.println is 527, the first is 480.

为什么差异的答案是 1 ?它应该是 1.097916667 ,我以为使用 double 会让我计算成小数?

Why is the answer for diff coming out as 1? It should be 1.097916667, I thought using a double would enable me to calculate into decimals?

推荐答案

划分两个 int s会得到一个 int ,然后隐式转换为 double 。在divison之前将一个投射到 double

Dividing two ints will get you an int, which is then implicitly converted to double. Cast one to a double before the divison:

double diff = (double)end / first;

这篇关于Java Division错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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