尝试编写Java程序来满足此要求:Duke Shirts出售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));
}
}
有人可以阐明为什么它不能正确编译吗?谢谢!
答案 0 :(得分:-1)
您在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));
}
}
}