总而言之,此代码应该询问客户姓名,会员级别和原始购买价格。它应该将所有这些存储在它们各自的变量中,如果输入了除接受的成员级别之外的其他内容,则程序应该退出。存储完所有变量后,客户的名称,会员级别,原始购买价格,促销价格和保存金额应以本文末尾的格式打印出来。在此先感谢您的帮助!
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作为customerName,Silver作为级别,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 */
}
}
答案 0 :(得分:0)
请参阅this。 试试这个语法:
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));
答案 1 :(得分:0)
现在你基本上将整个字符串"Original purchase price: $" + purchase
作为%.2f
的参数传递。请改用String.format
:
System.out.println("Original purchase price: $" + String.format("%.2f", purchase));
这也消除了将"\n"
附加到字符串的需要,因为您可以使用System.out.println
。