我正在尝试增加一个数组的值,该值与使用该折扣的次数相对应。如果折扣为0%(discount = 1.0),则递增效果很好,但对于20%,30%和40%(分别为discount is 0.8, 0.7, 0.6),counts数组中的相关索引将递增2。最后,如果 discount = 0.5递增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; }}这是一个示例输入:13311151019682910327129和相关的输出:Units sold: 13Discount: 19.999999999999996%Price: $1029.6000000000001Units sold: 31Discount: 30.000000000000004%Price: $2148.2999999999997Units sold: 115Discount: 50.0%Price: $5692.5Units sold: 101Discount: 50.0%Price: $4999.5Units sold: 96Discount: 40.0%Price: $5702.4Units sold: 8Discount: 0.0%Price: $792.0Units sold: 29Discount: 30.000000000000004%Price: $2009.6999999999998Units sold: 103Discount: 50.0%Price: $5098.5Units sold: 27Discount: 30.000000000000004%Price: $1871.1Units sold: 129Discount: 50.0%Price: $6385.5================== == DISCOUNTS == ==================0% discounts: 120% discounts: 230% discounts: 640% discounts: 250% discounts: 32如您所见,在输出中只有一个实例给出了0%的折扣。此外,每个实例只有20%和40%的折扣,但是输出显示每个实例2个,与30%的折扣相似。另外,有4个实例给出了50%的折扣,但是如您所见,数组增加了32次...这是我调用counts[4]的主程序。/* * 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(); }} (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 如果我正确获取您的代码,则该错误与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调用的数量,则应该避免对getter中的全局变量进行修改。可能将折扣计算移至构造函数,而getDiscount()仅返回先前在构造函数中计算出的折扣。但这只是一个说明。 (adsbygoogle = window.adsbygoogle || []).push({}); 10-08 19:37