简单的java披萨订购程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30108084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple java pizza order program
提问by Bryce Sickles
I am having trouble with a basic pizza order program. My final print ends up with orders that are always over $100 and i cant seem to figure out why other than it has something to do with numberOfToppings
variable. Maybe I'm missing something obvious, maybe not. Thanks for your help!
我在使用基本的披萨订购程序时遇到问题。我最终打印出来的订单总是超过 100 美元,我似乎无法弄清楚为什么除了它与numberOfToppings
变量有关之外。也许我错过了一些明显的东西,也许不是。谢谢你的帮助!
Here's the code
这是代码
public static void main(String[] args) {
DecimalFormat df = new DecimalFormat("#.##");
Scanner keyboard = new Scanner(System.in);
// Variables
String firstName; // first name of user
char crustType;
String crust; // name of crust
int inches; // pizza size
double cost = 0.0; // pizza cost
final double taxRate = 0.08; // amount tax owed
double tax; // tax amount
double total; // total of pizza + toppings
double lastTotal; // total of everything
int numberOfToppings = 0;
String toppings = "Cheese";
// Prompts for name & determines discount
System.out.println("Enter your name: ");
firstName = keyboard.nextLine();
//Prompts for distance
double distance = 0;
double deliveryfee = 0;
System.out.println("Please enter total distance in miles from pizza shop (0 for in store pickup):");
distance = keyboard.nextDouble();
if (distance == 0) {
deliveryfee = 0;
System.out.println("There is no delivery fee.");
} else if (distance > 1) {
deliveryfee = ((distance * 0.5) + 2);
System.out.println("Your delivery fee is: $" + df.format(deliveryfee));
} else if (distance > 0) {
deliveryfee = 2.00;
System.out.println("Your delivery fee is: $" + df.format(deliveryfee));
}
// Prompts for pizza size
System.out.print("What size of pizza would you like (diameter in inches)? (10, 12, 14, or 16) ");
inches = keyboard.nextInt();
if (inches == 10) {
cost = 10.99;
} else if (inches == 12) {
cost = 12.99;
} else if (inches == 14) {
cost = 14.99;
} else if (inches == 16) {
cost = 16.99;
} else if (inches != 10 && inches != 12 && inches != 14 && inches != 16) {
System.out.println("The number you have entered is illegal, your pizza size will be set to 12 inches. ");
cost = 12;
}
keyboard.nextLine();
// Prompts user for type of crust
System.out.print("What type of crust do you want? (H)and-Tossed, (T)hin-crust, or (D)eep-dish (enter H, T, or D,): ");
crustType = keyboard.nextLine().charAt(0);
if (crustType == 'H' || crustType == 'h') {
crust = "Hand-Tossed";
} else if (crustType == 'T' || crustType == 't') {
crust = "Thin-Crust";
} else if (crustType == 'D' || crustType == 'd') {
crust = "Deep-Dish";
} else if (crustType != 'H' && crustType != 'h' && crustType != 'T' && crustType != 't' && crustType != 'D' && crustType != 'd') {
System.out.println("The crust type you have entered is illegal, your crust type will be set to hand-tossed. ");
}
crust = "Hand-Tossed";
// Prompts user for additonal toppings
System.out.println("All pizzas come with cheese.");
System.out.println("Additional toppings are .25 each, choose from Pepperoni or Sausage.");
// Pepperoni
System.out.println("Do you want Pepperoni? (Y/N)");
numberOfToppings = keyboard.nextLine().charAt(0);
if (numberOfToppings == 'Y' || numberOfToppings == 'y') {
numberOfToppings = numberOfToppings + 1;
toppings = toppings + " and Pepperoni";
} else {
}
//Sausage
System.out.println("Do you want Sausage? (Y/N)");
numberOfToppings = keyboard.nextLine().charAt(0);
if (numberOfToppings == 'Y' || numberOfToppings == 'y') {
numberOfToppings = numberOfToppings + 1;
toppings = toppings + " and Sausage";
} else {
}
// Calculations
System.out.println(cost);
System.out.println(numberOfToppings);
System.out.println(deliveryfee);
total = (cost) + (numberOfToppings * 1.25) + (deliveryfee);
tax = total * taxRate;
lastTotal = total * (1 + taxRate);
// Payment Confirmation
System.out.println(firstName + ", here is your order:");
System.out.println(inches + " inch pizza");
System.out.println(crust + ", " + toppings);
System.out.println("Order Cost: $" + df.format(total));
System.out.println("Tax: $" + df.format(tax));
System.out.println("Total Due: $" + df.format(lastTotal));
}
采纳答案by Bill Horvath
You're using the numberOfToppings variable as both a means of tracking the number of toppings, and as a means of capturing command line input. If the user puts in (e.g.) M on the command line, numberOfToppings' final value will be 'M'. The solution is to use a different variable for the input, e.g.:
您将 numberOfToppings 变量用作跟踪浇头数量和捕获命令行输入的方法。如果用户在命令行输入(例如)M,numberOfToppings 的最终值将是'M'。解决方案是对输入使用不同的变量,例如:
int userInput = keyboard.nextLine().charAt(0);
if (userInput == 'Y' || userInput == 'y'){
...
回答by Raphael Amoedo
You're setting numberOfToppings everytime someone types a char.
每次有人输入字符时,您都在设置 numberOfToppings。
int numberOfToppings = 0;
numberOfToppings = 'Y' + 1;
System.out.println(numberOfToppings);
It returns 90, because 'Y' it's a char and it's converted to int value. If you change your code from this:
它返回 90,因为 'Y' 它是一个字符并且它被转换为 int 值。如果您从此更改代码:
numberOfToppings = keyboard.nextLine().charAt(0);
if (numberOfToppings == 'Y' || numberOfToppings == 'y' )
To this:
对此:
char character;
...
character = keyboard.nextLine().charAt(0);
if (character == 'Y' || character == 'y' )
It will works.
它会起作用。
回答by kamal
Here you go.. This is the code. I fixed it for you. Go ahead and just copy paste this and try it. It will work.
给你.. 这是代码。我给你修好了。继续,只需复制粘贴并尝试一下。它会起作用。
import java.text.DecimalFormat;
import java.util.Scanner;
public class pizzatest{
public static void main(String[] args){
DecimalFormat df = new DecimalFormat("#.##");
Scanner keyboard = new Scanner(System.in);
// Variables
String firstName; // first name of user
char crustType;
String crust; // name of crust
int inches; // pizza size
double cost = 0.0; // pizza cost
final double taxRate = 0.08; // amount tax owed
double tax; // tax amount
double total; // total of pizza + toppings
double lastTotal; // total of everything
int numberOfToppings = 0;
int numberOfToppings2 =0;
int numberOfToppings3;
String toppings = "Cheese";
// Prompts for name & determines discount
System.out.println("Enter your name: " );
firstName = keyboard.nextLine();
//Prompts for distance
double distance = 0;
double deliveryfee = 0;
System.out.println("Please enter total distance in miles from pizza shop (0 for in store pickup):");
distance = keyboard.nextDouble();
if (distance == 0){
deliveryfee = 0;
System.out.println("There is no delivery fee.");}
else if (distance > 1){
deliveryfee = ((distance * 0.5) +2);
System.out.println("Your delivery fee is: $" + df.format(deliveryfee));}
else if (distance > 0){
deliveryfee = 2.00;
System.out.println("Your delivery fee is: $" + df.format(deliveryfee));}
// Prompts for pizza size
System.out.print("What size of pizza would you like (diameter in inches)? (10, 12, 14, or 16) " );
inches = keyboard.nextInt();
if (inches == 10 ){
cost = 10.99; }
else if (inches == 12){
cost = 12.99;}
else if (inches == 14){
cost = 14.99;}
else if (inches == 16){
cost = 16.99;}
else if (inches != 10 && inches != 12 && inches != 14 && inches != 16){
System.out.println("The number you have entered is illegal, your pizza size will be set to 12 inches. " );
cost = 12;}
keyboard.nextLine();
// Prompts user for type of crust
System.out.print("What type of crust do you want? (H)and-Tossed, (T)hin-crust, or (D)eep-dish (enter H, T, or D,): " );
crustType = keyboard.nextLine().charAt(0);
if (crustType == 'H' || crustType == 'h' ){
crust = "Hand-Tossed";}
else if (crustType == 'T' || crustType == 't' ){
crust = "Thin-Crust";}
else if (crustType == 'D' || crustType == 'd' ){
crust = "Deep-Dish";}
else if (crustType != 'H' && crustType != 'h' && crustType != 'T' && crustType != 't' && crustType != 'D' && crustType != 'd' ){
System.out.println("The crust type you have entered is illegal, your crust type will be set to hand-tossed. " );}
crust = "Hand-Tossed";
// Prompts user for additonal toppings
System.out.println("All pizzas come with cheese." );
System.out.println("Additional toppings are .25 each, choose from Pepperoni or Sausage." );
// Pepperoni
System.out.println("Do you want Pepperoni? (Y/N)" );
numberOfToppings = keyboard.nextLine().charAt(0);
if (numberOfToppings == 'Y' || numberOfToppings == 'y' ){
numberOfToppings = 1;
toppings = toppings + " and Pepperoni";}
else{
numberOfToppings = 0;
}
//Sausage
System.out.println("Do you want Sausage? (Y/N)" );
numberOfToppings2 = keyboard.nextLine().charAt(0);
if (numberOfToppings2 == 'Y' || numberOfToppings2 == 'y' ){
numberOfToppings2 = 1;
toppings = toppings + " and Sausage";}
else{
numberOfToppings2 = 0;}
numberOfToppings3 = (numberOfToppings) + (numberOfToppings2);
// Calculations
total = (cost) + (numberOfToppings3 * 1.25) + (deliveryfee);
tax = total * taxRate;
lastTotal = total * ( 1 + taxRate );
// Payment Confirmation
System.out.println(firstName + ", here is your order:");
System.out.println(inches + " inch pizza");
System.out.println(crust +", " + toppings);
System.out.println("Order Cost: $" + df.format(total));
System.out.println("Tax: $" + df.format(tax));
System.out.println("Total Due: $" + df.format(lastTotal));
}
}