【发布时间】:2021-02-16 02:00:42
【问题描述】:
一家软件公司出售零售价为 99 美元的软件包。数量折扣如下表:
Quantity Discount
10-19 20%
20-49 30%
50-99 40%
100 or more 50%
编写一个程序,要求用户输入购买的包裹数量。然后程序应显示折扣金额(如果有)和折扣后的购买总额。例如,要计算值 N 的 20%,您可以使用以下公式:
`(20 / 100.0) * N (or 0.2 * N)`.
import java.util.Scanner;
public class SoftwareSales {
public static void main(String[] args) {
// declaring variables
int qtyPurchased;
double discount;
int cost = 99;
double total;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter number of packages purchased: ");
qtyPurchased = keyboard.nextInt();
keyboard.nextLine();
if (qtyPurchased >= 10 && qtyPurchased <= 19) {
discount = (0.2 * qtyPurchased);
} else if (qtyPurchased >= 20 && qtyPurchased <= 49) {
discount = (0.3 * qtyPurchased);
} else if (qtyPurchased >= 50 && qtyPurchased <= 99) {
discount = (0.4 * qtyPurchased);
} else if (qtyPurchased >= 100) {
discount = (0.5 * qtyPurchased);
} else if (qtyPurchased < 10) {
discount = 0 * qtyPurchased;
} else
discount = 0;
total = ((cost * qtyPurchased) - discount);
System.out.print("Your discount is: " + discount + "\n"
+ "Your total is: " + total);
}
}
**************************************************
Following is the error I get:
Given the following was entered from the keyboard:
0
you displayed:
Enter◦number◦of◦packages◦purchased:◦
**instead of:
Enter◦number◦of◦packages◦purchased:◦Your◦discount◦is:◦$0.00⏎
Your◦total◦is:◦$0.00⏎**
【问题讨论】:
-
无法复制。
Enter number of packages purchased: 0 Your discount is: 0.0 Your total is: 0.0
标签: java