[英]How to calculate the average and max/min of an array of calculations from input?
我正在尝试编写一个程序来读取项目及其价格,然后生成 output 报告客户花费最多和最少的金额,以及他们平均花费的金额和相应客户的姓名。
我无法计算准确的最大值/最小值/平均值。 我怀疑我的错误是在我计算客户购买的所有商品的总成本时(如我的评论所述)。
我意识到 hashmap 或使用课程可能更容易,但我还没有学会,而且我们的讲师不允许这样做,所以我正在努力使用我知道如何使用的技术。 (在这种情况下为循环和 arrays)
这就是我下面的内容:
import java.util.Scanner;
public class Store {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// Integer count of number of items in the store
int count = scan.nextInt();
// Create an array to store names and prices of each item
String[] itemName = new String[count];
double[] itemPrice = new double[count];
for (int i=0; i<count; i++) {
// Scan name of each item and price
itemName[i] = scan.next();
itemPrice[i] = scan.nextDouble();
}
// Integer count for the number of customers
int numCustomers = scan.nextInt();
String[] fName = new String[numCustomers];
String[] lName = new String[numCustomers];
double[] costs = new double[numCustomers];
double sum = 0;
double average = 0;
for (int j=0; j<numCustomers; j++) {
fName[j] = scan.next();
lName[j] = scan.next();
//Number of items bought
int numItems = scan.nextInt();
for (int k=0; k<numItems; k++) {
// For each number of items bought, name and quantity
int numItemBought = scan.nextInt();
String nameOfItem = scan.next();
// Find prices of the items customer bought
for (int i=0; i<numItems; i++) {
if (nameOfItem.equals(itemName[i])) {
double price = itemPrice[i];
// Calculate the total cost of all items purchased by customer
costs[i] = price * numItemBought;
}
}
}
}
// Calculate the sum and average
for (int l=0; l<costs.length; l++) {
sum = sum + costs[l];
average = sum/costs.length;
}
// Find the largest and smallest numbers
double temp;
for (int p=0; p<costs.length; p++) {
for (int j=p+1; j<costs.length; j++) {
if(costs[p]>costs[j]) {
temp = costs[p];
costs[p] = costs[j];
costs[j] = temp;
}
}
}
double smallest = costs[0];
double biggest = costs[costs.length-1];
System.out.println("Biggest: " + biggest + "\nSmallest: " + smallest + "\nAverage: " + average);
}
}
输入示例: 6
苹果 0.25
香蕉 0.75
牛奶 3.15
橙色 1.25
意大利腊肠 2.50
海绵1.15
3个
Carrie Brownstein 3 2 香蕉 1 橙子 2 牛奶
科林塔克 2 3 香蕉 2 海绵
J.net Weiss 1 5 意大利腊肠
Output:最大:6.3
最小:0.0
平均:2.85
应该在什么时候
最大:J.net Weiss (12.50)
最小的:科林塔克(4.55)
平均:8.70
问题 #1:您无法找到许多商品的价格,因为查找价格的循环有不正确的界限:
// Find prices of the items customer bought
for (int i=0; i<numItems; i++) {
应该:
// Find prices of the items customer bought
for (int i=0; i<count; i++) {
问题 #2: costs
数组只保留最后一项的成本,而不是将总成本相加,并且成本存储在item的索引下,而不是customer的索引下:
// Calculate the total cost of all items purchased by customer
costs[i] = price * numItemBought;
将客户的所有成本相加:
costs[j] = costs[j] + price * numItemBought;
这将有助于重命名索引变量,使它们有意义:例如customerIndex
而不是j
和itemIndex
而不是i
我认为您的代码中有很多需要修复的地方。 例如,在这个块中:
// Find prices of the items customer bought
for (int i=0; i<numItems; i++) {
if (nameOfItem.equals(itemName[i])) {
double price = itemPrice[i];
// Calculate the total cost of all items purchased by customer
costs[i] = price * numItemBought;
}
}
为什么要使用numItems
来循环其他所有目录,这是 1 位客户购买的商品数量。 如果我购买 1 件产品,您只会在循环中进行 1 次迭代,并且只会检查itemName
数组中的第一个产品(因此您很可能找不到我的产品,因此不会更新cost
。您应该有这样的东西:
for (int i=0; i<itemName.length; i++) {
if (nameOfItem.equals(itemName[i])) {
double price = itemPrice[i];
// Calculate the total cost of all items purchased by customer
costs[i] = price * numItemBought;
}
}
此行的另一个问题(实际上是 2 个):
costs[i] = price * numItemBought;
每次执行时,您都会用新的价格覆盖先前计算的价格。 但我会说你想要的是每个客户的所有项目的cost
。 所以你应该做一些像costs[i] = costs[i] + (price * numItemBought);
而且您没有为costs
数组使用正确的索引。 它被定义为一个数组,其中包含每个客户的成本。 所以你的索引应该代表一个客户。 但是您使用的是产品索引i
(如您所见,因为您正在执行itemName[i]
。所以您将一件事误认为另一件事并混合了您的数据。在您当前的代码中,它是j
代表客户,所以如果你结合我的 2 条评论,你的代码应该是costs[j] = costs[j] + (price * numItemBought);
当然还有其他问题需要解决,但这是第一步。
我的主要建议是尝试了解您创建的每个变量的确切含义。 如果对您来说更容易,请不要犹豫,给他们起更有意义的名字。 例如,用customerIndex
替换j
,用itemBoughtIndex
替换i
,用itemIndex
替换k
。 一旦您对循环和编程有了更多的经验,就可以 go 回到经典的“一个字母命名”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.