如何修正计算错误?

发布于 2024-11-14 22:36:44 字数 822 浏览 3 评论 0原文

我正在尝试编写一个程序,从用户那里读取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

多彩岁月 2024-11-21 22:36:44

在除法之前,将分子和分母转换为浮点数。

import java.util.Scanner;

public class divideByZero {

public static float quotient(float numerator, float 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();

    float result = quotient( (float) numerator, (float) denominator );

    System.out.printf("\n The first number %d divided by the second number "
            + "%d = %f\n", numerator, denominator, result );

}

Cast the numerator and denominator as floats before you divide them.

import java.util.Scanner;

public class divideByZero {

public static float quotient(float numerator, float 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();

    float result = quotient( (float) numerator, (float) denominator );

    System.out.printf("\n The first number %d divided by the second number "
            + "%d = %f\n", numerator, denominator, result );

}
双马尾 2024-11-21 22:36:44

以下更改将满足您的需要。

public static float quotient(int numerator, int denominator)
{
    return (float)numerator / (float)denominator;
}

float result = quotient( numerator, denominator );

System.out.printf("\n The first number %d divided by the second number "
        + "%d = %.2f\n", numerator, denominator, result2 );

你不能对 int 进行除法并得到小数 int (不存在这样的事情),因此必须使用 float 进行除法并存储到 float 中。 %.2f 将其限制为小数点后两位。

The following changes will do what you need.

public static float quotient(int numerator, int denominator)
{
    return (float)numerator / (float)denominator;
}

float result = quotient( numerator, denominator );

System.out.printf("\n The first number %d divided by the second number "
        + "%d = %.2f\n", numerator, denominator, result2 );

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.

夜血缘 2024-11-21 22:36:44

为什么不使用双精度数据类型而不是浮点数据类型?

下面是一些格式化为两位小数的代码:

import java.text.*;

public class DecimalPlaces {

   public static void main(String[] args) {

       double d = 1.234567;
       DecimalFormat df = new DecimalFormat("#.##");
       System.out.print(df.format(d));
   }

}

Why don't you use the datatype double instead of float?

Here is some code to format into two decimal points:

import java.text.*;

public class DecimalPlaces {

   public static void main(String[] args) {

       double d = 1.234567;
       DecimalFormat df = new DecimalFormat("#.##");
       System.out.print(df.format(d));
   }

}
瀟灑尐姊 2024-11-21 22:36:44

您得到 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.

眼眸 2024-11-21 22:36:44

如果将商方法的返回类型从 int 更改为 float,它将不再将结果四舍五入为最接近的整数。

public static float quotient(int numerator, int denominator)
{
   return (float) numerator / (float) denominator;
}

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.

public static float quotient(int numerator, int denominator)
{
   return (float) numerator / (float) denominator;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文