内部循环增量器增加了多个,为什么?

时间:2018-04-23 17:08:56

标签: java arrays

我正在尝试增加数组内部的值,该值对应于使用折扣的次数。如果折扣为0%(discount = 1.0),但对于20%,30%和40%(discount is 0.8, 0.7, 0.6分别),增量效果很好,counts数组中的相关索引会增加2.最后,如果discount = 0.5 counts[4]增加8,我觉得它与我所处的for循环的迭代有关,但我无法弄明白。

继承我认为存在问题的课程:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package softwaresales;
/**
 *
 * @author python
 */
public class SoftwareSales {
    private int unitsSold;
    private final double UNIT_PRICE = 99.0;
    private final int[] UNITS_LOW_RANGES = {1, 10, 20, 50, 100};
    private final double[] DISCOUNTS = {1.0, 0.8, 0.7, 0.6, 0.5};
    private static int[] counts = {0, 0, 0, 0, 0};

    SoftwareSales(int u){
        unitsSold = u;
    }

    public int getUnitsSold(){
        return unitsSold;
    }

    public double getDiscount(){
        double discount = 1;
        for (int i = 0; i < 4; i++){
            if((unitsSold >= UNITS_LOW_RANGES[i]) &&
                    (unitsSold < UNITS_LOW_RANGES[i+1])){
                counts[i] += 1;
                discount = DISCOUNTS[i];
            }
            else if (unitsSold >= 100){
                counts[4] += 1;
                discount = DISCOUNTS[4];
                System.out.print("*");
            }
        }
        return discount;
    }

    public double getCost(){
        return unitsSold * UNIT_PRICE * getDiscount();
    }

    public int[] getCounts(){
        return counts;
    }
}

以下是一个示例输入:

13
31
115
101
96
8
29
103
27
129

相关输出:

Units sold: 13
Discount: 19.999999999999996%
Price: $1029.6000000000001

Units sold: 31
Discount: 30.000000000000004%
Price: $2148.2999999999997

Units sold: 115
Discount: 50.0%
Price: $5692.5

Units sold: 101
Discount: 50.0%
Price: $4999.5

Units sold: 96
Discount: 40.0%
Price: $5702.4

Units sold: 8
Discount: 0.0%
Price: $792.0

Units sold: 29
Discount: 30.000000000000004%
Price: $2009.6999999999998

Units sold: 103
Discount: 50.0%
Price: $5098.5

Units sold: 27
Discount: 30.000000000000004%
Price: $1871.1

Units sold: 129
Discount: 50.0%
Price: $6385.5

=================
=               =
=   DISCOUNTS   =
=               =
=================
0% discounts: 1
20% discounts: 2
30% discounts: 6
40% discounts: 2
50% discounts: 32

正如您所看到的,只有一个实例给出了0%的折扣,该折扣在输出中表示。也只有一个实例,每个20%和40%的折扣,但输出显示每个2,类似于30%的折扣。还有4个实例给出了50%的折扣,但正如你所看到的那样,数组增加了32倍......

这是我的主程序,我打电话给getDiscount()

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package softwaresales;
import java.io.*;
import java.util.Scanner;

/**
 *
 * @author python
 */
public class SofwareSalesDriver {

    /**
     * @param args the command line arguments
     * @throws java.io.IOException
     */
    public static void main(String[] args) throws IOException {
        String file_location;
        Scanner kb = new Scanner(System.in);
        CreateInputFile inputs = new CreateInputFile();
        System.out.println("Enter the PATH to the folder where you would like" +
                " the in/out files: ");
        System.out.println("\nExamples:\nLinux: /home/%USER_NAME%/Documents/" + 
                "\nor Windows: C:\\\\users\\\\%USER_NAME%\\\\Documents\\\\");
        System.out.print("\nEnter PATH: ");
        file_location = kb.nextLine();
        String infile = file_location + "Inputs.txt";
        String outfile = file_location + "Outfile.txt";
        File file = new File(outfile);
        FileWriter writer = new FileWriter(file);
        int unitsSold = 0;
        SoftwareSales customer = new SoftwareSales(unitsSold);
        int[] counts = customer.getCounts();
        inputs.createInputFile(file_location);
        Scanner fileLine = new Scanner(new File(infile));
        while (fileLine.hasNextInt()){
            unitsSold = fileLine.nextInt();
            customer = new SoftwareSales(unitsSold);
            writer.write("Units sold: " + unitsSold + "\n" + 
                    "Discount: " + (1 - customer.getDiscount())*100 + "%\n" +
                    "Price: $" + customer.getCost() + "\n\n");
        }
        writer.write("=================\n=               =\n" +
                "=   DISCOUNTS   =\n=               =\n" +
                "=================\n" + 
                "0% discounts: "+ counts[0] / 2 +
                "\n20% discounts: " + counts[1] + 
                "\n30% discounts: " + counts[2] + 
                "\n40% discounts: " + counts[3] + 
                "\n50% discounts: " + counts[4] + "\n\n");

        writer.close();
    }
}

1 个答案:

答案 0 :(得分:1)

如果我正确获取了您的代码,则错误与if循环中的for - 语句有关。您应该在for循环之前进行检查,否则如果unitsSold >= 100,则每个循环多次递增计数器,因为每次循环迭代都会调用else语句。

if (unitsSold >= 100){
    counts[4] += 1;
    discount = DISCOUNTS[4];
    System.out.print("*");
} else {
    for (int i = 0; i < 4; i++){
        if((unitsSold >= UNITS_LOW_RANGES[i]) &&
                (unitsSold < UNITS_LOW_RANGES[i+1])){
            counts[i] += 1;
            discount = DISCOUNTS[i];
        }
    }
}

对某些数字进行重复计算的原因是由于该函数:

public double getCost(){
    return unitsSold * UNIT_PRICE * getDiscount();
}

在此,您再次致电getDiscount(),这将再次触发整个过程,并将相应的值添加到counts[i]

我建议您使用以下内容:您可以将折扣作为getCost(double discount)等参数传递,而不是计算两次折扣。 这样,您就可以防止两次调用此函数。

最后快速通知:通常,如果您不打算实际计算getter-calls的数量,则应避免在getter中执行全局变量的修改。也许,折扣计算可以移动到构造函数,getDiscount()只返回先前在构造函数中计算的折扣。但这只是一个侧面说明。