如何修正计算错误?
我正在尝试编写一个程序,从用户那里读取 2 个数字并将它们相除。这是我到目前为止的代码:
import java.util.Scanner;
public class divideByZero {
public static int quotient(int numerator, int denominator)
{
return numerator / denominator;
}
public static void main(String args[])
{
Scanner scanner = new Scanner(System.in);
System.out.print("Please enter the first number: ");
int numerator = scanner.nextInt();
System.out.print("Please enter the second number: ");
int denominator = scanner.nextInt();
int result = quotient( numerator, denominator );
float result2 = (float)result;
System.out.printf("\n The first number %d divided by the second number "
+ "%d = %f\n", numerator, denominator, result2 );
}
我在计算方面遇到问题。例如,当我输入 3 除以 4 时,得到的结果是 0.000000。如何得到精确到小数点后两位的正确结果?
I'm trying to write a program that reads 2 numbers from the user and divides them. Here is the code I have so far:
import java.util.Scanner;
public class divideByZero {
public static int quotient(int numerator, int denominator)
{
return numerator / denominator;
}
public static void main(String args[])
{
Scanner scanner = new Scanner(System.in);
System.out.print("Please enter the first number: ");
int numerator = scanner.nextInt();
System.out.print("Please enter the second number: ");
int denominator = scanner.nextInt();
int result = quotient( numerator, denominator );
float result2 = (float)result;
System.out.printf("\n The first number %d divided by the second number "
+ "%d = %f\n", numerator, denominator, result2 );
}
I'm having problems with the computations. For example, when I enter 3 divided by 4, I get the result 0.000000. How do I get the correct result to 2 decimal places?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在除法之前,将分子和分母转换为浮点数。
Cast the numerator and denominator as floats before you divide them.
以下更改将满足您的需要。
你不能对 int 进行除法并得到小数 int (不存在这样的事情),因此必须使用 float 进行除法并存储到 float 中。 %.2f 将其限制为小数点后两位。
The following changes will do what you need.
You cannot do division on an int and get a fractional int (there is no such thing), so the division must be done with floats and stored into a float. The %.2f limits it to 2 decimal places.
为什么不使用双精度数据类型而不是浮点数据类型?
下面是一些格式化为两位小数的代码:
Why don't you use the datatype double instead of float?
Here is some code to format into two decimal points:
您得到 0.0000,因为您正在除以整数并将结果存储回整数。要查看小数部分,可以将 result 声明为浮点变量。
当它起作用时,要打印 2 位小数,您可以使用 printf 格式。
在 C 中,它类似于 %.2f。
根据这些错误,我认为您会从阅读数据类型(尤其是浮点数)中受益。
You are getting 0.0000 because you are dividing integer numbers and storing that result back into an integer. To see the fractional part, you can declare result to be a float variable.
When that's working, to print 2 decimals you can use printf formatting.
In C it'd be something like %.2f.
Based on the errors, I think you'd benefit from reading about data types, especially floating point numbers.
如果将商方法的返回类型从 int 更改为 float,它将不再将结果四舍五入为最接近的整数。
If you change the return type of the quotient method from an int to a float, it will no longer round the answer to the nearest integer.