我正在尝试编写一个程序,从用户那里读取 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。如何获得正确的结果到小数点后 2 位?