试图写一个Java程序来满足这一点:杜克衬衫销售Java T恤$24.95每件,但折扣是可能的数量如下:1或2衬衫,没有折扣和总运费是$10.00 3-5衬衫,折扣是10%和总运费$8.00 6-10衬衫,折扣是20%和总运费是5.00美元11件或更多的衬衫,折扣是30%和送货是免费的写一个Java程序,提示用户所需的衬衫数量。然后,程序应该打印衬衫的扩展价格、运费和订单的总成本。在适当的情况下使用货币格式。
下面是我的代码:
import java.lang.Math;
import java.util.Scanner;
public class Excercise2_3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("How many shirts do you need?");
double shirts = input.nextInt();
if (shirts <= 2)
System.out.printf("The extended cost is : $%.2", (shirts * 24.95));
System.out.printf("The shipping charges are $10.00");
System.out.printf("The total cost is : $%.2", (shirts * 24.95) + 10);
if (shirts > 2 && shirts <= 5)
System.out.printf("The extended cost is : $%.2", ((shirts * 24.95)*.10));
System.out.printf("The shipping charges are $8.00");
System.out.printf("The total cost is : $%.2", ((shirts * 24.95)*.10) + 8);
if (shirts > 5 && shirts <= 10)
System.out.printf("The extended cost is : $%.2", ((shirts * 24.95)*.20));
System.out.printf("The shipping charges are $5.00");
System.out.printf("The total cost is : $%.2", ((shirts * 24.95)*.20) + 5);
if (shirts > 10)
System.out.printf("The extended cost is : $%.2", ((shirts * 24.95)*.00));
System.out.printf("Shipping is free!");
System.out.printf("The total cost is : $%.2", ((shirts * 24.95)*.30));
}
}
有人能解释一下为什么它不能正确编译吗?谢谢!
发布于 2018-10-22 08:12:14
if语句两边缺少大括号。另外,您的printf格式字符串应该是%.2f
。你错过了f
,你错过了换行符。
import java.util.Scanner;
public class TempApp {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("How many shirts do you need?");
double shirts = input.nextInt();
if (shirts <= 2) {
System.out.printf("The extended cost is : $%.2f\n", (shirts * 24.95));
System.out.printf("The shipping charges are $10.00\n");
System.out.printf("The total cost is : $%.2f\n", (shirts * 24.95) + 10);
}
if (shirts > 2 && shirts <= 5) {
System.out.printf("The extended cost is : $%.2f\n", ((shirts * 24.95) * .10));
System.out.printf("The shipping charges are $8.00\n");
System.out.printf("The total cost is : $%.2f\n", ((shirts * 24.95) * .10) + 8);
}
if (shirts > 5 && shirts <= 10) {
System.out.printf("The extended cost is : $%.2f\n", ((shirts * 24.95) * .20));
System.out.printf("The shipping charges are $5.00\n");
System.out.printf("The total cost is : $%.2f\n", ((shirts * 24.95) * .20) + 5);
}
if (shirts > 10) {
System.out.printf("The extended cost is : $%.2f", ((shirts * 24.95) * .00));
System.out.printf("Shipping is free!");
System.out.printf("The total cost is : $%.2f", ((shirts * 24.95) * .30));
}
}
}
https://stackoverflow.com/questions/52920989
复制相似问题