Java, item shop discount math error -
this program meant act shop, number being inputted corresponding item , quantity. catch if want 3 or more items receive 10% discount on purchase , decimals should truncated (staying within whole numbers of int data type). program run, the discount isn't calculated , stated 0 though program run. check out!
int item, longsword, shortsword, warhammer, ring, potion, itemcost, quantity, discount, totalcost, finalcost; system.out.print("item number: "); item = keyboard.nextint(); final int longsword = 120; final int shortsword = 90; final int warhammer = 80; final int ring = 150; final int potion = 10; itemcost = 0; if (item == 1) { itemcost = longsword; } if (item == 2) { itemcost = shortsword; } if (item == 3) { itemcost = warhammer; } if (item == 4) { itemcost = ring; } if (item == 5) { itemcost = potion; } system.out.print("quantity of item: "); quantity = keyboard.nextint(); totalcost = itemcost * quantity; system.out.println("total cost: " + totalcost); if (quantity >= 3) { discount = totalcost * (1/10); } else { discount = 0; } system.out.println("discount: " + discount);
you experiencing oh-so-common integer division issue.
discount = totalcost * (1/10);
1/10
0, discount
0. use instead:
discount = (int) (totalcost * (0.1));
Comments
Post a Comment