【问题标题】:Can;t figure out why java is ignoring my print statement after the if else statement无法弄清楚为什么 java 在 if else 语句之后忽略了我的 print 语句
【发布时间】: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


【解决方案1】:

去掉下面这行。它不是必需的,并提示输入未使用的输入。

  keyboard.nextLine();

你也可以把这是一个循环,检查输入直到输入一个负值。

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);
        while (true) {
            System.out.print("\nEnter number of packages purchased: ");
            qtyPurchased = keyboard.nextInt();
            if (qtyPurchased < 0) {
                break;
            }
            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);
        }
        System.out.print("Done");
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多