代码:
import java.util.Scanner;
public class sdusti00lab1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
double AA = 8.25;
double CA = 6.50;
double ACP = 9.00;
double CPC = 6.25;
int numA, numC;
double numSP, numLP;
Scanner keys = new Scanner(System.in);
System.out.print(" enter number of adults ");
numA = keys.nextInt();
System.out.println(" enter number of children ");
numC = keys.nextInt();
System.out.println(" enter number of small popcorn");
numSP = keys.nextDouble();
System.out.println(" enter number of large popcorn");
numLP = keys.nextDouble();
double AAddPrice = (numA*AA);
double CAddPrice = (numC*CA);
double ACPT = ((ACP*.094)*AA);
double CPCT = ((CPC*.094)*CA);
double SPTax = (ACP*.094);
double LPTax = (CPC*.094);
System.out.println("Adult admission "+"\t"+numA + "\t$" + AAddPrice);
System.out.println("Child admission "+"\t"+numC + "\t$" + CAddPrice);
System.out.println("Adult popcorn "+"\t\t"+ACP + "\t$" + ACPT);
System.out.println("Child popcorn "+"\t\t"+CPC + "\t$" + CPCT);
System.out.println("Tax "+"\t\t\t$"+ (SPTax + LPTax));
System.out.println("Total "+"\t\t\t$"+(AAddPrice+CAddPrice+CPCT+ACPT) );
}
}
我需要更改最后6行代码,以产生一个小数,该小数位于小数点后第二位,但我不知道该怎么做。
答案 0 :(得分:1)
使用System.out.printf
或System.out.format
执行此操作。使用%.2f
打印最多两个小数点。
System.out.printf("Adult admission \t%d\t$%.2f%n", numA, AAddPrice);
答案 1 :(得分:0)
有多种方法可以将数字(float,double,..)转换为格式化的字符串。 Oracle有一个很好的overview值得研究。
这里要理解的重要方面是:该过程由你工作
A)指定描述输出结果的格式
B)您使用该格式调用格式化程序以及要格式化的数字
答案 2 :(得分:-1)
使用针对system.out.format [here] [https://docs.oracle.com/javase/tutorial/java/data/numberformat.html]
所述的代码