Java%f仅返回0.000000

时间:2018-01-25 17:52:38

标签: java

我是Java新手。我编写了这个简单的程序,但由于某些原因,即使我使用转换说明符%f for float,商(除法计算)也会将命令窗口中的结果返回为0.000000。为什么这不能正确计算?以下是我编写该程序的问题。

/ * 2.15(算术)编写一个应用程序,要求用户输入两个整数,获取它们 来自用户并打印他们的总和,产品,差异和商(除法)。使用这些技术 如图2.7所示。 * /

import java.util.Scanner;

public class Main 
{
  public static void main ( String [] args )
  {
    Scanner input = new Scanner (System.in);

    int a;
    int b;

    int sum;
    int product;
    int difference;
    float quotient;

    System.out.println( "Enter the first integer: ");
    a = input.nextInt();

    System.out.println( "Enter the second integer: ");
    b = input.nextInt();

    sum = a + b;
    System.out.printf( "The sum is %d.%n", sum );

    product = a * b;
    System.out.printf( "The product is %d.%n", product );

    difference = a - b;
    System.out.printf ( "The difference is %d.%n", difference );

    quotient = a / b;

    System.out.printf ( "The quotient is %f.%n", quotient );
  }
}

0 个答案:

没有答案