java 如何编写解决折扣+价格的程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26070605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 09:14:58  来源:igfitidea点击:

How to write a program that solves discount + price

javaalgorithmpseudocode

提问by BornFailure

A software company sells a package that retails for $99. Quantity discounts are applied if the number of packages ordered is:

一家软件公司销售的软件包零售价为 99 美元。如果订购的包裹数量为:

Quantity              Discount
10 - 19                20%
20 - 49                30%
50 - 99                40%
100 or more            50%

Create an application SoftwareSales that prompts the user to enter the number of software packages ordered, calculates the cost of the purchase, and displays the result. The application can be console or GUI based.

创建一个应用程序 SoftwareSales,提示用户输入订购的软件包数量、计算购买成本并显示结果。该应用程序可以是基于控制台或 GUI 的。

How should I approach this?

我应该如何处理这个问题?

I know I need something like this in there?

我知道我需要这样的东西吗?

if(score < 10) {
    discount = 0.0
} else if(score < 20){
    discount = 0.1
}
// other cases go here

Anyone want to at least give me the frame/skeleton for a program like this?

有人想至少给我这样一个程序的框架/骨架吗?

回答by user3068854

import javax.swing.JOptionPane;    

int userInput = Integer.parseInt(JOptionPane.showInputDialog("Enter no. of packages: "));
double cost = 99;
double finalCost;

if(userInput>=10 && userInput<20){
    finalCost= cost - ((cost/100)*20)
} else if(userInput>=20 && userInput<50){
    finalCost= cost - ((cost/100)*30)
} else if( etc.)

That's the basic variables you'll need and all you need to do is fill in the conditions of each discount. You won't learn much if I gave you all the code, hope it helps!

这是您需要的基本变量,您需要做的就是填写每个折扣的条件。如果我给你所有的代码,你不会学到很多东西,希望它有帮助!

回答by Elvermg

You must use something like this:

你必须使用这样的东西:

if(score >= 10 && <= 19)
  {
      discount = 0.20;
  }
else if(score >= 20 && <= 49)
{
    discount = 0.30;
}
 And so on....

回答by Maurice Perry

I would use a table:

我会用一张桌子:

private static class Discount {
    private final int fromQuantity;
    private final double percent;

    public Discount(int fromQuantity, double percent) {
        this.fromQuantity = fromQuantity;
        this.percent = percent;
    }
}

public static final Discount[] DISCOUNT_TABLE = {
    new Discount(10, 20),
    new Discount(20, 30),
    new Discount(50, 40),
    new Discount(100, 50)
};

To get the discount percentage for a quantity:

要获取数量的折扣百分比:

public static double discountForQuantity(int quantity) {
    int i = 0;
    do {
        --i;
    } while (i >= 0 && DISCOUNT_TABLE[i].fromQuantity > quantity);
    return i < 0 ? 0 : DISCOUNT_TABLE[i].percent;
}

To calculate the price of some quantity of the product:

计算一定数量产品的价格:

public static double priceForQuantity(int quantity, double basePrice) {
    return quantity*basePrice*(1-(discountForQuantity(quantity)/100));
}