java 除号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43300892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dividing numbers
提问by oneoe
Division of two numbers using arithmetic operators in Java.
在 Java 中使用算术运算符对两个数字进行除法。
Scanner scan = new Scanner(System.in);
System.out.print("Write a number: ");
String mataett = scan.next();
System.out.print("Write a number: ");
String matatva = scan.next();
int nr1 = Integer.parseInt(mataett);
int nr2 = Integer.parseInt(matatva);
System.out.println(Math.round(nr1 / nr2*10.0)/10.0);
When I run this with 73/27
I get the answer 2.0
, but what I want to get is the answer 2.7
当我运行它时,73/27
我得到了答案2.0
,但我想得到的是答案2.7
回答by Ousmane D.
When I run this with 73/27 I get the answer 2.0, but what I want to get is the answer 2.7
当我用 73/27 运行这个时,我得到了答案 2.0,但我想要得到的是答案 2.7
The reason being you're getting the unexpected result is because when you divide by integers, the result is always an integer, which means any number after the decimal point is truncated.
你得到意想不到的结果的原因是,当你除以整数时,结果总是一个整数,这意味着小数点后的任何数字都会被截断。
nr1 / nr2 // <-- this part within the calculation is causing the problem.
A simple trick which you can do to overcome the problem is to multiply one of the integers nr1
or nr2
with 1.0
first then that should produce a double which should allow you to keep the precision because you're dividing by double
now rather than int
.
您可以用来解决这个问题的一个简单技巧是将其中一个整数相乘nr1
或nr2
与1.0
first then相乘,这应该会产生一个 double ,这应该可以让您保持精度,因为您正在除以double
now 而不是int
。
Example:
例子:
System.out.println(Math.round((1.0 * nr1) / nr2 * 10.0)/10.0);
Notice the 1.0
请注意 1.0
回答by Donat Pants
Change the line
换线
System.out.println(Math.round(nr1 / nr2*10.0)/10.0);
To the line
到线
System.out.println(Math.round(nr1 / ((double)nr2)*10.0)/10.0);
Because when you divide two integers, the answer is always an integer which is always rounder down ((int)2.9 = 2 and (int)3.1 = 3). cast into doubles and then divide in order to get the right answer.
因为当你将两个整数相除时,答案总是一个整数,它总是向下取整((int)2.9 = 2 和 (int)3.1 = 3)。转换成双打,然后分开以获得正确的答案。
Let's look at the following code:
让我们看看下面的代码:
int a = 10;
int b = 3;
System.out.println(a/b);
This will print 3, it will not print 3.3333333... that is because when dividing integers the result is always an integer and always rounded down. There are several ways to solve this
这将打印 3,它不会打印 3.3333333... 那是因为当除以整数时,结果总是一个整数并且总是向下取整。有几种方法可以解决这个问题
int a = 10;
double b = 3;
System.out.println(a/b);
This will print 3.3333333... because we are no longer dividing 2 integers, one of the variables is a double and therefor the result will be a double
这将打印 3.3333333... 因为我们不再除以 2 个整数,其中一个变量是双精度数,因此结果将是双精度数
Other ways:
其他方法:
Cast into a double:
int a = 10; int b = 3; System.out.println((double)a/b);
Multiplying an integer with a double will result in a double and therefor this will also work
int a = 10; int b = 3; System.out.println(1.0*a/b);
转换成双:
int a = 10; int b = 3; System.out.println((double)a/b);
将整数与双精度相乘将产生双精度,因此这也适用
int a = 10; int b = 3; System.out.println(1.0*a/b);
Notice that this will not work
请注意,这将不起作用
int a = 10;
int b = 3;
System.out.println(a/b * 1.0);
This will not work because the division will be calculated before the multiplication and you will get the result 3.0 .
这将不起作用,因为将在乘法之前计算除法,您将得到结果 3.0 。
回答by cheyma
Try the following:
请尝试以下操作:
double nr1 = Integer.parseInt(mataett);
double nr2 = Integer.parseInt(matatva);