My java program only prompts the user but does not read the data. How can I make my code actually read the data and perform the calculations? -
my program supposed ask user if order pizza , user supposed enter
yes or no
(all lower case)
entering no exit program entering yes make computer prompt user again information-
last name
(one word, no need validate)
choice of pizza type
(options veggie, cheese, pepperoni, , supreme)
choice of pizza size
(options small, medium, , large)
once have entered of information program supposed print following information
customer last name
(whatever entered in)
cost of pizza
total number of large pizzas
total number of medium pizzas
total number of small pizzas
average cost of order
when compiled program there no errors , when ran prompted perfectly...however, after type in answers program starts on again , prompts beginning. not output or calculations. doesn't check if enter correct. i'm not sure went wrong? logic, code, or both? can please show me how fix this. thank you!
oh, , used notepad++ , compiled , ran in command prompt if helpful information.
/*this program keep prompting user enter pizza order, perform requested calculation, , output requested result. written hannah lane*/ import java.util.scanner; public class pizzaorders { public static void main(string[] args) { scanner input = new scanner(system.in); int smallpizzas = 0, mediumpizzas = 0, largepizzas = 0, numberoforders = 0; double totalordercost = 0.0, pizzacost = 0.0, averagecost = 0.0; string custlastname = "", pizzasize = "", pizzatype ="", response = ""; /*the loop prompt user see if customer order pizza. if yes, prompt user last name, choice of pizza type, , choice of pizza size. 2 possible responses yes , no. dummy value loop no. */ system.out.print("do want order 1 pizza?" + "type yes or no (all lower case), press enter key."); response = input.next(); while (!(response.equals("no"))) { /*validate user's response. if valid, prompt required values, perform calculation, , output result. if invalid, output error message. */ if (response.equals("yes")) { system.out.println("please type in last name (it can 1 word) , press enter key."); custlastname = input.next(); system.out.println("please type in choice of pizza in lower case letters. type keyboard must be" + " pepperoni, veggie, cheese, or supreme."); pizzatype = input.next(); system.out.println("please type in choice of pizza size in lower case letters. type keyboard must" + " small, medium, or large."); pizzasize = input.next(); /* validate entries calculation. sizes must small, medium, or large. types of pizza must pepperoni, veggie, cheese, or supreme. division, must make sure denominator not zero. if invalid, output error message. */ if (pizzatype.equals("pepperoni") || pizzatype.equals("veggie") || pizzatype.equals("cheese") || pizzatype.equals("supreme") && pizzasize.equals("small") || pizzasize.equals("medium") || pizzasize.equals("large") && numberoforders !=0.0) { if (pizzatype.equals ("pepperoni")) { if (pizzasize.equals ("small")) { smallpizzas = smallpizzas + 1; pizzacost = 8.50; totalordercost = totalordercost + 8.50; numberoforders = numberoforders + 1; } else if (pizzasize.equals ("medium")) { mediumpizzas = mediumpizzas + 1; pizzacost = 9.50; totalordercost = totalordercost + 9.50; numberoforders = numberoforders + 1; } else if(pizzasize.equals ("large")) { largepizzas = largepizzas + 1; pizzacost = 10.50; totalordercost = totalordercost + 10.50; numberoforders = numberoforders + 1; } } else if (pizzatype.equals ("veggie")) { if (pizzasize.equals ("small")) { smallpizzas = smallpizzas + 1; pizzacost = 10.00; totalordercost = totalordercost + 10.00; numberoforders = numberoforders + 1; } else if (pizzasize.equals ("medium")) { mediumpizzas = mediumpizzas + 1; pizzacost = 12.25; totalordercost = totalordercost + 12.25; numberoforders = numberoforders + 1; } else if (pizzasize.equals ("large")) { largepizzas = largepizzas + 1; pizzacost = 14.50; totalordercost = totalordercost + 14.50; numberoforders = numberoforders + 1; } } else if (pizzatype.equals ("cheese")) { if (pizzasize.equals ("small")) { smallpizzas = smallpizzas + 1; pizzacost = 7.00; totalordercost = totalordercost + 7.00; numberoforders = numberoforders + 1; } else if (pizzasize.equals ("medium")) { mediumpizzas = mediumpizzas + 1; pizzacost = 8.00; totalordercost = totalordercost + 8.00; numberoforders = numberoforders + 1; } else if (pizzasize.equals ("large")) { largepizzas = largepizzas + 1; pizzacost = 9.00; totalordercost = totalordercost + 9.00; numberoforders = numberoforders + 1; } } else if (pizzatype.equals ("supreme")) { if (pizzasize.equals ("small")) { smallpizzas = smallpizzas + 1; pizzacost = 11.00; totalordercost = totalordercost + 11.00; numberoforders = numberoforders + 1; } else if (pizzasize.equals ("medium")) { mediumpizzas = mediumpizzas + 1; pizzacost = 14.00; totalordercost = totalordercost + 14.00; numberoforders = numberoforders + 1; } else if (pizzasize.equals ("large")) { largepizzas = largepizzas + 1; pizzacost = 16.00; totalordercost = totalordercost + 16.00; numberoforders = numberoforders + 1; } averagecost = totalordercost/(double)numberoforders; system.out.println(custlastname + pizzacost + smallpizzas + mediumpizzas + largepizzas + averagecost); } } } else system.out.println("what have typed in incorrect. response must yes or no."); system.out.println("do want order 1 pizza? type yes or no" + "(all lower case), press enter key."); response = input.next(); } } }
your code print out order executed in case user buying supreme small pizza. check location of print line it's within if-else structure. should move bit lower (just before last else). check code below, moved following line around:
system.out.println(custlastname + pizzacost + smallpizzas + mediumpizzas + largepizzas + averagecost);
complete code:
import java.util.scanner; public class pizzaorders { public static void main(string[] args) { scanner input = new scanner(system.in); int smallpizzas = 0, mediumpizzas = 0, largepizzas = 0, numberoforders = 0; double totalordercost = 0.0, pizzacost = 0.0, averagecost = 0.0; string custlastname = "", pizzasize = "", pizzatype ="", response = ""; /*the loop prompt user see if customer order pizza. if yes, prompt user last name, choice of pizza type, , choice of pizza size. 2 possible responses yes , no. dummy value loop no. */ system.out.print("do want order 1 pizza?" + "type yes or no (all lower case), press enter key."); response = input.next(); while (!(response.equals("no"))) { /*validate user's response. if valid, prompt required values, perform calculation, , output result. if invalid, output error message. */ if (response.equals("yes")) { system.out.println("please type in last name (it can 1 word) , press enter key."); custlastname = input.next(); system.out.println("please type in choice of pizza in lower case letters. type keyboard must be" + " pepperoni, veggie, cheese, or supreme."); pizzatype = input.next(); system.out.println("please type in choice of pizza size in lower case letters. type keyboard must" + " small, medium, or large."); pizzasize = input.next(); /* validate entries calculation. sizes must small, medium, or large. types of pizza must pepperoni, veggie, cheese, or supreme. division, must make sure denominator not zero. if invalid, output error message. */ if (pizzatype.equals("pepperoni") || pizzatype.equals("veggie") || pizzatype.equals("cheese") || pizzatype.equals("supreme") && pizzasize.equals("small") || pizzasize.equals("medium") || pizzasize.equals("large") && numberoforders !=0.0) { if (pizzatype.equals ("pepperoni")) { if (pizzasize.equals ("small")) { smallpizzas = smallpizzas + 1; pizzacost = 8.50; totalordercost = totalordercost + 8.50; numberoforders = numberoforders + 1; } else if (pizzasize.equals ("medium")) { mediumpizzas = mediumpizzas + 1; pizzacost = 9.50; totalordercost = totalordercost + 9.50; numberoforders = numberoforders + 1; } else if(pizzasize.equals ("large")) { largepizzas = largepizzas + 1; pizzacost = 10.50; totalordercost = totalordercost + 10.50; numberoforders = numberoforders + 1; } } else if (pizzatype.equals ("veggie")) { if (pizzasize.equals ("small")) { smallpizzas = smallpizzas + 1; pizzacost = 10.00; totalordercost = totalordercost + 10.00; numberoforders = numberoforders + 1; } else if (pizzasize.equals ("medium")) { mediumpizzas = mediumpizzas + 1; pizzacost = 12.25; totalordercost = totalordercost + 12.25; numberoforders = numberoforders + 1; } else if (pizzasize.equals ("large")) { largepizzas = largepizzas + 1; pizzacost = 14.50; totalordercost = totalordercost + 14.50; numberoforders = numberoforders + 1; } } else if (pizzatype.equals ("cheese")) { if (pizzasize.equals ("small")) { smallpizzas = smallpizzas + 1; pizzacost = 7.00; totalordercost = totalordercost + 7.00; numberoforders = numberoforders + 1; } else if (pizzasize.equals ("medium")) { mediumpizzas = mediumpizzas + 1; pizzacost = 8.00; totalordercost = totalordercost + 8.00; numberoforders = numberoforders + 1; } else if (pizzasize.equals ("large")) { largepizzas = largepizzas + 1; pizzacost = 9.00; totalordercost = totalordercost + 9.00; numberoforders = numberoforders + 1; } } else if (pizzatype.equals ("supreme")) { if (pizzasize.equals ("small")) { smallpizzas = smallpizzas + 1; pizzacost = 11.00; totalordercost = totalordercost + 11.00; numberoforders = numberoforders + 1; } else if (pizzasize.equals ("medium")) { mediumpizzas = mediumpizzas + 1; pizzacost = 14.00; totalordercost = totalordercost + 14.00; numberoforders = numberoforders + 1; } else if (pizzasize.equals ("large")) { largepizzas = largepizzas + 1; pizzacost = 16.00; totalordercost = totalordercost + 16.00; numberoforders = numberoforders + 1; } averagecost = totalordercost/(double)numberoforders; } } system.out.println(custlastname + pizzacost + smallpizzas + mediumpizzas + largepizzas + averagecost); } else system.out.println("what have typed in incorrect. response must yes or no."); system.out.println("do want order 1 pizza? type yes or no" + "(all lower case), press enter key."); response = input.next(); } } }
Comments
Post a Comment