jave - 使用简单的 Java 程序时遇到问题
问题描述
尝试编写一个 Java 程序来满足这一点: Duke Shirts 以每件 24.95 美元的价格出售 Java 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));
}
}
谁能解释为什么它不能正确编译?谢谢!
解决方案
您缺少 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));
}
}
}
推荐阅读
- pdf - 附件PDF文件(日文)通过VPN上传后损坏
- plugins - 使用 ExtendScript 在 After Effects 中重新加载预渲染预览
- google-apps-script - 部署的 Google Apps 脚本插件只能通过单独安装工作
- python - 如何在 CSV 文件中找到 isnull() 值?
- node.js - mongoose 查询 API 的困惑
- html - HTML列表在CSS中分成相等大小的块
- amazon-web-services - 使用 terraform 创建 lambda 事件源映射时访问被拒绝错误
- mysql - 如何修复 MySql 查询中的“不可见”语法错误?(在 TRIGGER 中使用 UPDATE)
- php - 如何从这样的数组列表中选择一个单词?
- outlook - 使用 tcl twapi 获取“无法获取 coclass 的 ITypeInfo:对象不支持 IProvideClassInfo 并且未指定 clsid。” 错误(office365)