编译程序后,我很难摆脱零。该程序将如下所示:
输入便士的数量:456 456便士等于8.0美元和76.0美分。我一直难以摆脱结尾处的.0和末尾也没有零的美分,因此它显示如下:
输入的便士的数量:456
456便士等于8美元和76美分。

import.java.util.Scanner;
import java.util.*;
import java.text.DecimalFormat;
public class Blah

{

 public static void main(String[] args)
 {
  int pennies;
  double dollars;
  double cents=0;
  Scanner input=new Scanner(System.in);
  DecimalFormat decimalFormat = new DecimalFormat("0.#");

  System.out.print("Enter the amount of pennies: ");

  pennies=input.nextInt();

  dollars = pennies/100 - cents;
  cents = pennies % 100;

  if(dollars<0)
         dollars = -dollars;

  System.out.println(pennies + "pennies equals " + dollars + "dollars and " + cents + " cents.");

   }
}

最佳答案

您已经创建了DecimalFormat,现在该使用它了

System.out.println(pennies + " pennies equals "
        + decimalFormat.format(dollars) + " dollars and "
        + decimalFormat.format(cents) + " cents.");

10-08 06:53