[英]Why does my sum suddenly decrease?
I have a program that tracks the number of items and cost of the items that you place in a shopping bag and then prints out the number of items, total cost of the items, cost + tax, and average. 我有一个程序,可以跟踪您放入购物袋中的商品数量和成本,然后打印出商品数量,商品总成本,成本+税金和平均值。
When I run the program and I continue to add items it will calculate everything accurately, but then suddenly the total cost will drop down. 当我运行该程序并继续添加项目时,它将能够准确地计算出所有内容,但是突然总成本就会下降。 ex: i add 2 items at 10 dollars each it prints: Items: 2 Total Cost: 20, but then I will add 1 item at 5 dollars and it will print: Items: 3 Total Cost: 15 rather than 25. 例如:我以每张10美元的价格添加2个项目:项目:2总成本:20,但是我将以5美元添加1个项目,并且它将打印:项目:3总成本:15,而不是25。
I've tried running it multiple times. 我尝试多次运行它。 It seems like if I keep adding items at 10 dollars it is accurate, but if I change it up, the total cost decreases. 如果我继续以10美元的价格添加商品,这似乎是准确的,但是如果我更改它,总成本就会降低。 It seems some what random as far as when the value decreases. 值减小时似乎有些随机。
I thought maybe there was some issue with using float, but I can't find anything to verify that. 我以为使用float可能存在一些问题,但我找不到任何东西可以验证这一点。
Here is the shopping bag class 这是购物袋类
public class ShoppingBag {
public float taxRate;
public int items;
public float cost;
public float average;
public float totalCost;
public float finalCost;
public ShoppingBag(float taxRate)
{
this.taxRate = taxRate;
}
public void place(int newitems, float newcost)
{
items = newitems;
cost = newcost;
cost = items * cost;
}
public int getItems()
{
return items;
}
public float getCost()
{
return cost;
}
public float getTotal()
{
finalCost = cost + (cost * taxRate);
return finalCost;
}
public float getAverage()
{
average = finalCost/items;
return average;
}
@Override
public String toString()
{
return("Items: " + items + " Cost: " + cost + " Final cost: " + finalCost + " Average cost: " + average);
}
}
Here is my main program 这是我的主程序
import java.util.Scanner;
public class ShoppingBagTracker {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
float taxRate, cost;
int items, newItems, choice;
String receipt;
String menu = ("1. Add items" +
"2. Get receipt"
+ "3. Exit");
System.out.print("Enter the sales tax rate: ");
taxRate = in.nextFloat();
ShoppingBag myBag = new ShoppingBag(taxRate);
items = 0;
do{
System.out.println("What would you like to do?");
System.out.println(menu);
choice = in.nextInt();
switch(choice){
case 1:
System.out.print("Enter cost of item: ");
cost = in.nextFloat();
System.out.print("Enter number of items: ");
newItems = in.nextInt();
items = items + newItems;
myBag.place(items, cost);
myBag.getItems();
myBag.getCost();
myBag.getTotal();
myBag.getAverage();
break;
case 2:
receipt = myBag.toString();
System.out.println(receipt);
break;
case 3:
break;
default:
System.out.println("That is not an option");
}
}while(choice != 3);
}
}
Your place
method overwrites the previous cost. 您的place
方法会覆盖之前的费用。
So when you call : 因此,当您致电:
myBag.place(items, cost);
the number of items is correct (since you update it before the call), but the previous cost is lost : 项数是正确的(因为您在致电之前进行了更新),但是先前的费用丢失了:
public void place(int newitems, float newcost)
{
items = newitems;
cost = newcost;
cost = items * cost;
}
You should probably change it to : 您可能应该将其更改为:
public void place(int newitems, float newcost)
{
items += newitems;
cost += newitems * newcost;
}
And pass to it just the new items count : 并将新项目数传递给它:
myBag.place(newitems, cost);
Have a look at the line 看看线
cost = items * cost;
cost is the cost of the most recent item added ($5) times the number of items (3), so cost is assigned 3*$5 => $15. cost是最近添加的商品的成本($ 5)乘以商品数(3),因此成本分配为3 * $ 5 => $ 15。
You will need to, either keep track of the prices of all the items added to the shopping cart, or keep a running total of the cost and add the cost of the items being added. 您将需要要么跟踪添加到购物车中的所有项目的价格,要么保持成本的总和并添加要添加的项目的成本。
Your ShoppingBag.place(int,float)
method doesn't add the new items and cost to the bag. 您的ShoppingBag.place(int,float)
方法不会将新商品和成本添加到ShoppingBag.place(int,float)
袋中。 It replaces what's in the bag, so the contents of the bag at any one time are only what you provided in the most recent call to place
. 它取代了袋子中的东西,因此在任何时候袋子中的内容仅是您在最新的place
呼叫中提供的内容。
In the main
method, you're incrementing items
before calling place
, but then place
multiplies the incremented value of items
by only the most recent value of cost
. 在main
方法中,您要在调用place
之前增加items
,但是place
将items
的增加值乘以cost
最新值。
Also, these two lines in your main
method have no effect, since the called methods just return the existing value of a field, and you never assign the returned value to anything: 同样,您的main
方法中的这两行也不起作用,因为被调用的方法只会返回字段的现有值,并且您绝不会将返回值分配给任何东西:
myBag.getItems();
myBag.getCost();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.