0

全体として、このコードは、顧客の名前、メンバー レベル、元の購入価格を要求することになっています。それらのすべてをそれぞれの変数に保存する必要があります。受け入れられたメンバーレベル以外の何かが入力された場合、プログラムは終了するはずです。すべての変数が保存された後、顧客の名前、メンバー レベル、元の購入価格、プロモーション価格、および節約された金額が、この投稿の最後にある形式で出力されます。助けてくれてありがとう!

import java.util.*;
public class Discount{
  public static void main(String[] args){
    Scanner keyboard = new Scanner(System.in);
    double purchase = 0.0;
    double platinum = (purchase * .2);
    double gold = (purchase * .15);
    double silver = (purchase * .1);
    String Platinum = null;
    String Gold = null;
    String Silver = null;
    String customerName = null;
    String level = null;
    double discount = 0.0;

    System.out.print("Please enter a customer name: ");
    customerName = keyboard.nextLine();
    System.out.print("Please enter the customer's member level: ");
    level = keyboard.nextLine();

    if (!level.equals("Platinum") && !level.equals("Gold") && !level.equals("Silver"))
    {
        System.exit(0);
    }

    System.out.print("Please enter the origianl purchase price: ");
    purchase = keyboard.nextDouble();

    System.out.println("Congratualations, " + customerName + "!");

    if (level.equals("Platinum") && purchase > 500)
    {
        discount = (platinum + (purchase*.05));
        System.out.println("As a " + level + " level cardholder, you have received a 25% discount during Bedlam.");
    }
    else if (level.equals("Platinum"))
    {
        discount = (platinum);
        System.out.println("As a " + level + " level cardholder, you have received a 20% discount during Bedlam.");
    }
    else if (level.equals("Gold"))
    {
        discount = (gold);
        System.out.println("As a " + level + " level cardholder, you have received a 15% discount during Bedlam.");
    }
    else if (level.equals("Silver"))
    {
        discount = (silver);
        System.out.println("As a " + level + " level cardholder, you have received a 10% discount during Bedlam.");
    }

この時点まではすべて問題なく動作します。顧客名として M、レベルとしてシルバー、購入として 500.20 を入力すると、コマンド プロンプトに次のように出力されます。 Congratualations, M! As a Silver level cardholder, you have received a 10% discount during Bedlam. Exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.String at java.util.Formatter$FormatSpecifier.failConversion(Unknown Source) at java.util.Formatter$FormatSpecifier.printFloat(Unknown Source) at java.util.Formatter$FormatSpecifier.print(Unknown Source) at java.util.Formatter.format(Unknown Source) at java.io.PrintStream.format(Unknown Source) at java.io.PrintStream.printf(Unknown Source) at Discount.main(Discount.java:62)

    System.out.printf("%.2f\n", "Original purchase price: $" + purchase);
    System.out.printf("%.2f\n", "Promotional price: $" + discount);
    System.out.printf("%.2f\n", "Amount saved: $" + (purchase-discount));

正常に実行された場合は、次のようになります。

Congratulations, Marge Simpson!
As a Silver level cardholder, you received a 10% discount during Bedlam.
Original purchase price: $500.20
Promotional price: $450.18
Amount saved: $50.02 */
    }
}
4

2 に答える 2

0

これを参照してください。次の構文を試してください。

System.out.println( "Original purchase price: $" + String.format("%.2f",purchase));
System.out.println("Promotional price: $" + String.format("%.2f",discount));
System.out.println("Amount saved: $" + String.format("%.2f", purchase-discount));
于 2013-09-21T04:01:40.993 に答える