代码工作正常。只是在寻找有关如何设置我的照片f并获得所需结果的信息。
签署多个打印f语句:
import java.util.Scanner;
public class Coupon {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
double total = 0;
double rate = 0;
double coupon = 0;
System.out.print ( " Please enter the cost of your groceries:") ;
total = input.nextDouble();
if (total < 10)
rate = 0;
else if (total > 10.01 && total <=60 )
rate = .08;
else if (total > 60.01 && total <=150 )
rate = .10;
else if (total > 150.01 && total <=210 )
rate = .12;
else if (total > 210.01)
rate = .14;
coupon = total*rate;
System.out.printf(" You win a discount coupon worth $ %3.2f\n",coupon, "(",rate, "% of your" ,total, "purchase");
}
}
您将赢得价值$ 1.12的折扣券。 (占购买价格的8%)
答案 0 :(得分:2)
如何设置printf?
第一个参数应该是一个格式字符串,其中带有一定数量的格式说明符(在此处称为 N )(以百分比开头),标记要插入值的位置,后跟 N < / em>提供要插入的值的参数。
如果要插入优惠券,价格和购买,您的通话应该大致类似于
System.out.printf(" ... coupon worth $%3.2f (%3.1f %% of your total purchase $%4.2f",
coupon, rate, purchase);
有3个%f
格式说明符和3个参数。请注意,文字百分号加倍。
我猜在合适的字段宽度上。