In: Computer Science
Hello,
I Have create this code and Tried to add do while loop but it gives me the error in string answar; and the areas where I blod
So cloud you please help me to do ( do while ) in this code.
// Program objective: requires user to input the data, program runs through the data,calcualtes the quantity, chacks prices for each iteam intered,calautes the prices seperatly for each item, and calculates the amount due without tax, and then give total amount due with tax included.
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); //initialise a Scanner object, and get all the information about items quantity and toppings ordered and the amount of money tendered as mentioned in questions.
string answar;
do {
System.out.print("\nEnter quantity of Small pizzas: ");// allows you to enter the quantuty of the samll pizza
int numSmallPizzas = sc.nextInt();//initialise a Scanner object for small pizza
System.out.print("Enter quantity of Small pizza toppings: "); // allows you to enter the topoings for small pizza.
int numSmallPizzaToppings = sc.nextInt();// allows you to enter the small toppings.
System.out.print("Enter quantity of Large pizzas: ");// allows you to enter the quantity of large pizza.
int numLargePizzas = sc.nextInt();//initialise a Scanner object for lage pizza.
System.out.print("Enter quantity of Large pizza toppings: ");// allows you to enter the Toppings for large pizza.
int numLargePizzaToppings = sc.nextInt();//Allows you to enter the toppings for large pizza.
System.out.print("Enter quantity of Soft Drinks: "); // allows you to enter the quantity of soft drinks.
int numSoftDrinks = sc.nextInt(); //initialise a Scanner object for sum of Soft drinks
System.out.print("Enter Amount tendered: ");
double amountTendered = sc.nextDouble();//initialise a Scanner object for totalAmount.
//stored the rates of all items
double smallPizzasRate = 8.99; // rate of small pizza
double smallPizzaToppingsRate = 1.5; // rate of the small pizza toppings
double largePizzasRate = 12.99; //rate of large pizza
double largePizzaToppingsRate = 2.5; // rate of the large pizza toppings
double softDrinksRate = 1.5; // rate of soft drinks
//find the amount for all items
double amountSmallPizzas = numSmallPizzas*smallPizzasRate; // calculates the amount of the small pizza.
double amountSmallPizzaToppings = numSmallPizzaToppings*smallPizzaToppingsRate; // calculates the amount of Toppings for small pizza.
double amountLargePizzas = numLargePizzas*largePizzasRate;// calculates the amount of large.
double amountLargePizzaToppings = numLargePizzaToppings*largePizzaToppingsRate; //calculates the amount of toppinf for large pizza
double amountSoftDrinks = numSoftDrinks*softDrinksRate;// calcualtes the amount for soft drinks intered.
//find totalAmount,salesTax, amountDueWithTax and change
double totalAmount = amountSoftDrinks + amountLargePizzaToppings + amountLargePizzas + amountSmallPizzas + amountSmallPizzaToppings;
double salesTax = totalAmount*0.06;
double amountDueWithTax = totalAmount + salesTax;
double change = amountTendered - amountDueWithTax;
//prints the receipt
System.out.println("\nWelcome to Yassir Pizza!"); //prints the title of the busniess
System.out.println("Address 111 Yassir Road Largo MD 20783 "); // print the address of the busniess
System.out.println("busniess phone number: (240)241-3462"); // prrints Busniess phone number
System.out.println("Your Order Was");
System.out.printf("%d Small pizzas @ %.4f each: $%.4f\n",numSmallPizzas,smallPizzasRate,amountSmallPizzas);
System.out.printf("%d Small pizza toppings @ %.4f each: $%.4f\n",numSmallPizzaToppings,smallPizzaToppingsRate,amountSmallPizzaToppings);
// prints out the total amount of small pizza and its toppings.
System.out.printf("%d Large pizzas @ %.4f each: $%.4f\n",numLargePizzas,largePizzasRate,amountLargePizzas);
System.out.printf("%d Large Pizza Toppings @ %.4f each: $%.4f\n",numLargePizzaToppings,smallPizzaToppingsRate,amountLargePizzaToppings);
// prints out the total amount of large pizza and its toppings.
System.out.printf("%d Soft Drinks @ %.4f each: $%.4f\n",numSoftDrinks,softDrinksRate,amountSoftDrinks);
// prints out the total amount of Softdrinks.
System.out.printf("\nTotal cost: $%.4f\n", totalAmount); // prints totalAmount due without tax
System.out.printf("Sales tax amount is: $%.4f\n", salesTax); // prints the salesTax
System.out.printf("Total amount due: $%.4f\n", amountDueWithTax);// prints the total amount due including tax.
System.out.printf("Amount tendered: $%.3f\n", amountTendered);// prints the amount given to casherir.
System.out.printf("Change: $%.4f\n", change);// prints the change for the receipt
System.out.println("\nThank you! \nYour order is complete!");
//print the complete statment.
if (totalAmount < amountTendered) {
System.out.println("\nThank you! \nYour order is complete!");
}
else
System.out.println("\nYour money is not enough");
System.out.println("do you want to add anything else? Yes or No");
answar = sc.next();
}
while (answar.equals("Yes"));
}
}
Solution :
Java is a case sensitive language. So there is a difference between "Hello" and "hello".
You are getting the error because you have written"string" instead of "String" in the statement : "string answar;"
Following is the corrected code :
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); //initialise a Scanner object, and get all the information about items quantity and toppings ordered and the amount of money tendered as mentioned in questions.
String answar;
do {
System.out.print("\nEnter quantity of Small pizzas: ");// allows you to enter the quantuty of the samll pizza
int numSmallPizzas = sc.nextInt();//initialise a Scanner object for small pizza
System.out.print("Enter quantity of Small pizza toppings: "); // allows you to enter the topoings for small pizza.
int numSmallPizzaToppings = sc.nextInt();// allows you to enter the small toppings.
System.out.print("Enter quantity of Large pizzas: ");// allows you to enter the quantity of large pizza.
int numLargePizzas = sc.nextInt();//initialise a Scanner object for lage pizza.
System.out.print("Enter quantity of Large pizza toppings: ");// allows you to enter the Toppings for large pizza.
int numLargePizzaToppings = sc.nextInt();//Allows you to enter the toppings for large pizza.
System.out.print("Enter quantity of Soft Drinks: "); // allows you to enter the quantity of soft drinks.
int numSoftDrinks = sc.nextInt(); //initialise a Scanner object for sum of Soft drinks
System.out.print("Enter Amount tendered: ");
double amountTendered = sc.nextDouble();//initialise a Scanner object for totalAmount.
//stored the rates of all items
double smallPizzasRate = 8.99; // rate of small pizza
double smallPizzaToppingsRate = 1.5; // rate of the small pizza toppings
double largePizzasRate = 12.99; //rate of large pizza
double largePizzaToppingsRate = 2.5; // rate of the large pizza toppings
double softDrinksRate = 1.5; // rate of soft drinks
//find the amount for all items
double amountSmallPizzas = numSmallPizzas*smallPizzasRate; // calculates the amount of the small pizza.
double amountSmallPizzaToppings = numSmallPizzaToppings*smallPizzaToppingsRate; // calculates the amount of Toppings for small pizza.
double amountLargePizzas = numLargePizzas*largePizzasRate;// calculates the amount of large.
double amountLargePizzaToppings = numLargePizzaToppings*largePizzaToppingsRate; //calculates the amount of toppinf for large pizza
double amountSoftDrinks = numSoftDrinks*softDrinksRate;// calcualtes the amount for soft drinks intered.
//find totalAmount,salesTax, amountDueWithTax and change
double totalAmount = amountSoftDrinks + amountLargePizzaToppings + amountLargePizzas + amountSmallPizzas + amountSmallPizzaToppings;
double salesTax = totalAmount*0.06;
double amountDueWithTax = totalAmount + salesTax;
double change = amountTendered - amountDueWithTax;
//prints the receipt
System.out.println("\nWelcome to Yassir Pizza!"); //prints the title of the busniess
System.out.println("Address 111 Yassir Road Largo MD 20783 "); // print the address of the busniess
System.out.println("busniess phone number: (240)241-3462"); // prrints Busniess phone number
System.out.println("Your Order Was");
System.out.printf("%d Small pizzas @ %.4f each: $%.4f\n",numSmallPizzas,smallPizzasRate,amountSmallPizzas);
System.out.printf("%d Small pizza toppings @ %.4f each: $%.4f\n",numSmallPizzaToppings,smallPizzaToppingsRate,amountSmallPizzaToppings);
// prints out the total amount of small pizza and its toppings.
System.out.printf("%d Large pizzas @ %.4f each: $%.4f\n",numLargePizzas,largePizzasRate,amountLargePizzas);
System.out.printf("%d Large Pizza Toppings @ %.4f each: $%.4f\n",numLargePizzaToppings,smallPizzaToppingsRate,amountLargePizzaToppings);
// prints out the total amount of large pizza and its toppings.
System.out.printf("%d Soft Drinks @ %.4f each: $%.4f\n",numSoftDrinks,softDrinksRate,amountSoftDrinks);
// prints out the total amount of Softdrinks.
System.out.printf("\nTotal cost: $%.4f\n", totalAmount); // prints totalAmount due without tax
System.out.printf("Sales tax amount is: $%.4f\n", salesTax); // prints the salesTax
System.out.printf("Total amount due: $%.4f\n", amountDueWithTax);// prints the total amount due including tax.
System.out.printf("Amount tendered: $%.3f\n", amountTendered);// prints the amount given to casherir.
System.out.printf("Change: $%.4f\n", change);// prints the change for the receipt
System.out.println("\nThank you! \nYour order is complete!");
//print the complete statment.
if (totalAmount < amountTendered) {
System.out.println("\nThank you! \nYour order is complete!");
}
else
System.out.println("\nYour money is not enough");
System.out.println("do you want to add anything else? Yes or No");
answar = sc.next();
}
while (answar.equals("Yes"));
}
}
Code demo for reference :
Output :