My output should be Enter price of item you are buying 12 (my input) With 6.0% t
ID: 3748161 • Letter: M
Question
My output should be Enter price of item you are buying
12 (my input)
With 6.0% tax, purchase is $12.00.72 (the purchase price should be $12.72)
What am I doing wrong? My code is below.
import java.util.Scanner;
public class DebugTwo4
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String costString;
double cost;
double TAX = .06;
System.out.println("Enter price of item you are buying");
costString = input.next();
cost = Double.parseDouble(costString);
System.out.println("With " + TAX *100 +
"% tax, purchase is $" + cost + cost * TAX);
}
Explanation / Answer
import java.util.Scanner;
public class DebugTwo4
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String costString;
double cost,tot_cost;
double TAX = .06;
System.out.println("Enter price of item you are buying");
costString = input.next();
cost = Double.parseDouble(costString);
System.out.println("With " + TAX *100 +
"% tax, purchase is $" + (cost+cost*TAX));
}
}
your doing wrong while printing output what you are doing is your concatenate string with cost first thats why your getting 12.00 and the 0.72 is added to that
what you have to do is first calculate actual cost and concatenate with output string.For that i have put open braces to the (cost+cost *TAx) according to percedence of operators () will execute first and concatenae with string using +;
you can do like below also
tot_cost=cost+cost*TAX;
System.out.println("With " + TAX *100 +
"% tax, purchase is $" + tot_cost);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.