Java(如何在输出中添加小数)

时间:2014-02-05 03:58:17

标签: java

//我不能有任何双打,只有int。我得到了正确的结果,但是可以添加小数?而不是150,我想说1.50。感谢。

import java.util.Scanner;

public class coinsInJar
{
    public static void main(String[] args)
    {
        int quarters, dimes, nickels, pennies, totalValue, totalValue2; 
        Scanner scan = new Scanner(System.in);


        System.out.print ("Enter the number of quarters: "); 
        quarters = scan.nextInt();

        System.out.print ("Enter the number of dimes: ");
        dimes = scan.nextInt();

        System.out.print ("Enter the number of nickels: "); 
        nickels = scan.nextInt();

        System.out.print ("Enter the number of pennies: "); 
        pennies = scan.nextInt();

        totalValue = (quarters * 25) + (dimes * 10) + (nickels * 5) + (pennies * 1);

        System.out.print ("Total value: $" + totalValue);

    }
}

1 个答案:

答案 0 :(得分:4)

打印整数值,然后打印点,然后打印其余部分:

System.out.print ("Total value: $" + totalValue/100 + "." + totalValue%100);