所以我需要制作一个计算折扣的计算器。我不知道我错过了我的代码没有运行,我需要它来读取输入的金额并分配正确的折扣(100或更少5%,超过100低于250 10%,250或更高15%)然后在折扣和税后产生总金额 感谢您的任何帮助或建议=)
/*
Sarah Goldberg Week 5 3 part discount
*/
import java.util.*;
public class Discount5 {
public static final double TAXRATE = 0.06;
public static void main(String[] args) {
double amount, tax, subTotal, due;
final double discount;
Scanner input = new Scanner(System.in);
System.out.println("Total due calculation program");
//input
System.out.print("Enter the sales amount: $");
amount = input.nextDouble();
//discount
if (amount <= 100){
discount = amount * 0.05;
}
else if (amount > 250 & amount < 100) {
discount = amount * 0.10;
}
else (amount >= 250) {
discount = amount * 0.15;
}
subTotal = amount - discount;
tax = subTotal * TAXRATE;
due = subTotal + tax;
// output
System.out.println("Bill Summary:");
System.out.printf(" sales amount: $%2.2f\n", amount);
System.out.printf(" discount: $%2.2f\n", discount);
System.out.printf(" tax (at %2.2f%%): $%2.2f\n",(TAXRATE*100),tax);
System.out.printf(" Total due: $%2.2f\n" ,due);
input.close();
}
}
答案 0 :(得分:2)
你的&amp;&amp;运算符错误,你不应该在else中写任何条件。
if (amount <= 100){
discount = amount * 0.05;
}
else if (amount < 250 && amount > 100) {
discount = amount * 0.10;
}
else {
discount = amount * 0.15;
}
如果您的折扣在if-else块中发生变化并且未初始化,则您不能将折扣用作final
变量,您也需要初始化它。
这将有效 -
/*
Sarah Goldberg Week 5 3 part discount
*/
import java.util.*;
public class Discount5 {
public static final double TAXRATE = 0.06;
public static void main(String[] args) {
double amount, tax, subTotal, due;
double discount = 0;
Scanner input = new Scanner(System.in);
System.out.println("Total due calculation program");
//input
System.out.print("Enter the sales amount: $");
amount = input.nextDouble();
//discount
if (amount <= 100){
discount = amount * 0.05;
}
else if (amount < 250 && amount > 100) {
discount = amount * 0.10;
}
else if(amount>=250) {
discount = amount * 0.15;
}
subTotal = amount - discount;
tax = subTotal * TAXRATE;
due = subTotal + tax;
// output
System.out.println("Bill Summary:");
System.out.printf(" sales amount: $%2.2f\n", amount);
System.out.printf(" discount: $%2.2f\n", discount);
System.out.printf(" tax (at %2.2f%%): $%2.2f\n",(TAXRATE*100),tax);
System.out.printf(" Total due: $%2.2f\n" ,due);
input.close();
}
}
答案 1 :(得分:1)
&安培;&安培;运算符错误
else if (amount > 100 && amount < 250 ) {
discount = amount * 0.10;
}
并且折扣不应该是最终的