-2

これを満たす Java プログラムを作成しようとしています: Duke Shirts は Java T シャツを 1 枚 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));

    }

}

正しくコンパイルされない理由を誰かが明らかにすることはできますか? ありがとう!

4

1 に答える 1

-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));
        }
    }
}
于 2018-10-22T00:12:13.527 に答える