首页 > 解决方案 > 使用简单的 Java 程序时遇到问题

问题描述

尝试编写一个 Java 程序来满足这一点: Duke Shirts 以每件 24.95 美元的价格出售 Ja​​va T 恤,但以下数量可能有折扣: 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));

    }

}

谁能解释为什么它不能正确编译?谢谢!

标签: jave

解决方案


您缺少 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));
        }
    }
}

推荐阅读