简体   繁体   English

递增数组和长度

[英]incrementing arrays and length

I am setting up a shop program. 我正在建立一个商店计划。 The code below is a method. 下面的代码是一种方法。 The code includes setting up a shop and actually using the shop. 该代码包括建立商店和实际使用商店。 Within this method, I am trying to figure out the discount a user gets based upon how many items a user buys. 在这种方法中,我试图找出用户根据用户购买的商品数量获得的折扣。

When a user sets up a shop, they are able to set x amount of items to qualify for the discount. 当用户设置商店时,他们可以设置x数量的商品以获得折扣。

For example, if a user buys 20 items, and x amount of items to qualify for the discount is set to 6 packs. 例如,如果用户购买了20个商品,并且有资格获得折扣的x个商品数量被设置为6个包装。 The user would get (2 * the price of items) discount subtracted from the original price. 用户将从原始价格中减去(2 *项目的价格)折扣。

Every 6 packs the user gets 1 pack for free to a max of 20. 每6包,用户可免费获得1包,最多20包。

6 packs + 1 pack free + another 6 packs + one pack free + 6 pack = 20 packs. 6包+ 1包免费+另外6包+一包免费+ 6包= 20包。

I have tried to create loop such as subtracting the number of items a user wants to buy bought while counting value when modulo is 0 and subtracting from the amount that the user bought but I am getting nowhere close to it. 我试图创建循环,例如减去用户想要购买的商品数量,同时在模数为0时计算值,并从用户购买的金额中减去,但我无法接近它。

Arrays were brought from other methods: 数组来自其他方法:

price[i] is the price of the item buyItems[i] is the number of items a user buys packs[i] is the user choice of discount, for example, a user can set the discount to apply at 2 pack, 3 pack or even none. price[i]是商品的价格buyItems[i]是用户购买packs[i]的商品数量packs[i]是用户选择的折扣,例如,用户可以设置折扣适用于2包,3包甚至没有。

public static void checkOuts(String [] names, double [] price,double [] packs,double addDiscount[], double[] buyItems, double[]addDiscountrate, int k ){

    double orgSub=0;
    double newSub=0;
    double addPercent=0;
    double specDis =0;
    double freePack=0;
    double disCheck =0;
    double count=0;

    for (int i =0; i < k; i++ ) {
        orgSub+=price[i]*buyItems[i];
    }

    System.out.println("Original Subtotal:             $" + orgSub);

for (int i =0; i < k; i++ ) {
    orgSub+=price[i]*buyItems[i];
}

System.out.println("Original Subtotal:             $" + orgSub);

for (int j = 0; j < buyItems.length; j++) {
    disCheck = buyItems[j];

    for(int d =0; d < buyItems.length; d++) {
        freePack = packs[d];

        for (int s =1; s < disCheck; s++)
            if (s % freePack==0) {
                count++;
                disCheck = disCheck -1;
                System.out.println(disCheck);
            }

    }
    specDis+= count*price[j];
    // the final discount that will be subtracted from original
}

Why are all of these arrays? 为什么所有这些阵列? Aren't they just an amount? 它们不仅仅是金额吗? (I may be misunderstanding) (我可能会误解)

If you do change these to integers, you can get the amount for free with 如果您确实将这些更改为整数,则可以免费获得金额

count = Math.floor(buyItems/packs);

My version of your code (I removed things that were never explained): 我的代码版本(我删除了从未解释过的东西):

public static void checkOuts(String[] names, double[] price, int[] packs, int[] buyItems) {

        System.out.println("Name\tnumFree\ttotalNum\tTotalPrice");
        for (int i = 0; i < names.length; i++) {
            int numFree = buyItems[i] / (packs[i] + 1);
            double totalPrice = (buyItems[i] - numFree) * price[i];
            System.out.println(names[i] + "\t" + numFree + "\t" + buyItems[i] + "\t\t" + totalPrice);
        }

    }

When called with the following code: 使用以下代码调用时:

String[] names = {"Coke", "Pepsi"};
double[] price = {1, 1};
int[] packs = {3, 4};
int[] buyItems = {10, 20};

checkOuts(names, price, packs, buyItems);

You get the following output: (Which is in line with your comments) 您将获得以下输出:(这与您的评论一致)

Name    numFree totalNum TotalPrice
Coke    2       10       8.0
Pepsi   4       20       16.0

You may also want to look into making your code object oriented. 您可能还希望研究使您的代码对象。 Rather than passing around parallel arrays, it would be easier to pass around a single array of items, or orders, which would have fields for name, price, discount, and anything else you needed. 它不是绕过并行数组,而是传递一个项目或订单数组,这些数据包含名称,价格,折扣以及您需要的任何其他内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM