我正在做练习,因为我真的需要快速改进我的java。该应用程序用于比萨饼订购计划,我需要将大部分用户输入保留在变量中直到结束,但它们将返回null
和0
。我显然做错了什么可以有人启发我从我自己的代码中学得最好,阅读其他例子可能会让我感到困惑。我需要从比萨饼大小,比萨饼皮和浇头等方法中的变量显示在收据中方法
import java.util.Scanner;
public class test {
;
public static final double TEN_INCH_PRICE=10.99;
public static final double TWELVE_INCH_PRICE=12.99;
public static final double FOURTEEN_INCH_PRICE=14.99;
public static final double SIXTEEN_INCH_PRICE=16.99;
public static final double TOPPING_COST=1.25;
public static void main(String[] args) {
int pizzaChoice = 0;
String pizzaSize = null;
int crustChoice;
int toppingChoice;
String crustWanted = null;
String toppingWanted = null;
String customerName;
double pizzaPrice = 0;
displayWelcome();
pizzaSize(pizzaSize);
pizzaCrust(crustWanted);
toppingChoice(toppingWanted);
pizzaCost(pizzaChoice, pizzaPrice);
receipt(pizzaSize, crustWanted, toppingWanted,pizzaPrice);
}
/**
* Display Welcome Message
* */
public static void displayWelcome() {
System.out.println("******************************************");
System.out.println("* CIT Pizza *");
System.out.println("******************************************");
}
public static String getname() {
return null;
}
public static String pizzaSize(String pizzaSize){
System.out.println("Pizza Size (inches) Cost");
System.out.println("=================== ====");
System.out.println("10 10.99");
System.out.println("12 12.99");
System.out.println("14 14.99");
System.out.println("16 16.99 ");
String message = "Please enter the size of pizza you want";
int pizzaChoice=readValidPizzaSize(message); //Get pizza size choice
if (pizzaChoice==10)
pizzaSize="10 inch pizza";
if (pizzaChoice==12)
pizzaSize="12 inch pizza";
if (pizzaChoice==14)
pizzaSize="14 inch pizza ";
if (pizzaChoice==16)
pizzaSize="16 inch pizza";
return pizzaSize;
}
public static String pizzaCrust(String crustWanted){
System.out.println("Pizza Crust Choice");
System.out.println("==================");
System.out.println("All crusts cost the same price.");
System.out.println("1 - Hand-tossed");
System.out.println("2 - Thin-crust");
System.out.println("3 - Deep-dish");
String message = ("Please enter crust choice");
int crustChoice=readValidCrustChoice(message);
if (crustChoice==1)
crustWanted="Hand-Tossed";
if (crustChoice==2)
crustWanted="Thin-crust";
if (crustChoice==3)
crustWanted="Deep-dish";
return crustWanted;
}
public static String toppingChoice(String toppingWanted){
System.out.println("Pizza Topping Choice");
System.out.println("====================");
System.out.println("All pizzas come with cheese and tomato sauce.");
System.out.println("An additional topping costs €1.25.");
System.out.println("Choose from:");
System.out.println("1 - Pepperoni");
System.out.println("2 - Sausage");
System.out.println("3 - Onion");
System.out.println("4 - None");
String message = ("Please enter topping choice");
int toppingChoice=readValidToppingChoice(message);
if (toppingChoice==1)
toppingWanted="Pepperoni";
if (toppingChoice==2)
toppingWanted="Sausage";
if (toppingChoice==3)
toppingWanted="Onion";
if (toppingChoice==4)
toppingWanted="None";
return toppingWanted;
}
public static double pizzaCost(int pizzaChoice, double pizzaPrice){
if (pizzaChoice==10)
pizzaPrice=TEN_INCH_PRICE;
if (pizzaChoice==12)
pizzaPrice=TWELVE_INCH_PRICE;
if (pizzaChoice==14)
pizzaPrice=FOURTEEN_INCH_PRICE;
if (pizzaChoice==16)
pizzaPrice=SIXTEEN_INCH_PRICE;
return pizzaPrice;
}
public static void receipt(String pizzaSize, String crustWanted, String toppingWanted,double pizzaPrice){
System.out.println("Order Details");
System.out.println("=============");
System.out.println("your order is as follows:");
System.out.println(pizzaSize);
System.out.println(crustWanted);
System.out.println("Cheese,Tomato sauce,"+ " "+toppingWanted);
System.out.println("The cost of your order is:"+ " "+pizzaPrice);
}
public static int readInt(String prompt)
{
// Create a scanner object for input from the console window
Scanner keyboard = new Scanner(System.in);
// boolean flag which is used to control the
// data validation loop
boolean numGood = false; // Assume the worst
do
{
System.out.print(prompt); // ask for the value
if(!keyboard.hasNextInt()) // check if what's in the keyboard buffer is not a double
{
System.out.println("You must enter an value!"); // display an error message
keyboard.nextLine(); // consume the bad value entered
}
else
numGood = true; // value entered is good
} while(!numGood);
// at this point we know the value in the
// keyboard buffer is numeric so we can go ahead and
// return it.
return keyboard.nextInt();
}
/**
* The readValidPizzaSize method reads an applicable pizza size
* from the console window and will display an appropriate
* error message if a non-applicable int value is entered.
* @param prompt A prompt to request the user to enter a pizza size
* @return The applicable int value entered.
*/
public static int readValidPizzaSize(String prompt)
{
int value;
do
{
value = readInt(prompt); // ask for and read an double value
if (value !=10 && value !=12 && value!=14 && value!=16) // check if the value entered is applicable
// display an error message
System.out.println("Error - you must enter a valid pizza size");
} while (value !=10 && value !=12 && value!=14 && value!=16);
// at this point we know the value entered is positive
// so return it
return value;
}
public static int readValidCrustChoice(String prompt)
{
int value;
do
{
value = readInt(prompt); // ask for and read an double value
if (value !=1 && value !=2 && value!=3) // check if the value entered is applicable
// display an error message
System.out.println("Error - you must enter a valid crust choice");
} while (value !=1 && value !=2 && value!=3);
// at this point we know the value entered is positive
// so return it
return value;
}
public static int readValidToppingChoice(String prompt)
{
int value;
do
{
value = readInt(prompt); // ask for and read an double value
if (value !=1 && value !=2 && value!=3 &&value!=4) // check if the value entered is applicable
// display an error message
System.out.println("Error - you must enter a valid topping choice");
} while (value !=1 && value !=2 && value!=3 && value!=4);
// at this point we know the value entered is positive
// so return it
return value;
}
}
答案 0 :(得分:0)
只需将主要方法更改为:
public static void main(String[] args) {
int pizzaChoice = 0;
String pizzaSize = null;
int crustChoice;
int toppingChoice;
String crustWanted = null;
String toppingWanted = null;
String customerName;
double pizzaPrice = 0;
displayWelcome();
pizzaSize = pizzaSize(pizzaSize);
pizzaChoice = Integer.parseInt(pizzaSize.replaceAll("[^0-9]", ""));
crustWanted = pizzaCrust(crustWanted);
toppingWanted = toppingChoice(toppingWanted);
pizzaPrice = pizzaCost(pizzaChoice, pizzaPrice);
receipt(pizzaSize, crustWanted, toppingWanted,pizzaPrice);
}
问题在于,无论何时向String分配新文本,实际上都是在Java中创建一个新的String对象。此外,Java的pass-by-reference传递了引用的副本。因此,您要将新对象分配给引用的副本;因此,您的原始字符串不受影响。您可能想要研究如何在Java中传递对象和原始类型。