I have this idea for my assignment where I wanted a cash register system to calculate the total for an item when the user enters it's cost price and quantity of said item.
That 开发者_如何学Pythonseemed to work, but then led my main problem - I wanted to let the user type the letter "T" after say, 10 transactions, to find out the total takings for the day.
I tried to use a for loop with the BigDecimal math class within the calculations etc. I have errors on the words 'valueOf' within my calculations & Eclipse keeps trying to change my values to 'long' & i'm pretty sure that's not right.
My explanation isnt amazing so i'll give you the code i wrote and place comments next to where my errors are ..
try{
Scanner in = new Scanner (System.in);
String t = "T";
int count;
for (count = 1;count<=10;count++){
System.out.println("\n\nValue of Item " + count + " :");
BigDecimal itemPrice = in.nextBigDecimal();
System.out.println("Quantity of item " + count + " :");
BigDecimal itemQuantity = in.nextBigDecimal();
BigDecimal itemTotal = (BigDecimal.valueOf(itemPrice).multiply // error here
(BigDecimal.valueOf(itemQuantity))); // error here
System.out.println("\nTotal for item(s): £" + itemTotal);
count++;
while (t == "T"){
BigDecimal amountOfItems = (BigDecimal.valueOf(itemTotal).divide // error here
(BigDecimal.valueOf(itemQuantity))); // error here
BigDecimal totalTakings = (BigDecimal.valueOf(itemTotal).multiply // error here
(BigDecimal.valueOf(amountOfItems))); // error here
System.out.println("The Total Takings For Today is £" + totalTakings + " " );
}
}
}
}
}
Like I said, the 'red lines' that eclipse uses to show there is an error are only under the words, "valueOf" within my BigDecimal calculations.
Any help would be great because i'm tearing my hair out !!!!
Thanx,
Vinnie.
There's no method BigDecimal.valueOf(BigDecimal)
. itemPrice
and itemQuantity
are already BigDecimal
values - you don't need any conversion:
BigDecimal itemTotal = itemPrice.multiply(itemQuantity);
EDIT: Okay, so the above solves your immediate problem, but you've also got:
while (t == "T")
{
// Loop that never changes the value of t
}
There are two issues with this:
- The loop will always either execute forever, not execute at all, or keep going until an exception is thrown, because the loop condition can never change as you're not changing the value of
t
. My guess is you wantt = System.in.readLine()
at some point... You're comparing two string references here whereas I suspect you want to compare their values, e.g.
while (t.equals("T"))
精彩评论