if语句中的初始化变量,稍后在代码中使用,但表示未定义 import java.util.Scanner; 导入java.lang.Math; 公共类比萨饼 { 公共静态void main(字符串[]args) { 双倍形; 双层浇头; 双层披萨皮; 双基成本; 双倍面积; 两倍的浇头; 一个顶部的最终双倍成本=0.025; 面团的最终双倍成本=0.019; 酱汁的最终双倍成本=0.036; 双倍直径的比萨; 双倍长度; 双倍宽的比萨; 双倍容积; 最后的双薄和脆=0.1; 最终双盘=0.5; 最终双经典手抛=0.25; 最后的双德州土司=0.9; 双粒锈; 双干酪锈菌; 材料的最终双重成本=0.02; 双份比萨饼; 交付成本加倍; 双重征税; 双层比萨饼; 最终双PI=3.14159; 双壳型; 双倍成本; 双倍的生产成本; 扫描仪键盘=新扫描仪(System.in); System.out.println(“你好,客户!欢迎来到Guiseppi's Just Pizza!”); System.out.println(“我们为您制作比萨饼的地方!”); System.out.println(“\n您想要什么样的比萨饼”+ “今天天气真好?”; 做 { System.out.print(“按1表示方形比萨饼,”); System.out.print(“或按2表示圆形比萨饼:”); shapeOfPizza=keyboard.nextDouble(); if(shapeoppizza!=1&&shapeoppizza!=2) { System.out.println(“输入不正确,请输入1或2”); } } while(shapeOfPizza!=1&&shapeOfPizza!=2); if(shapeOfPizza==1) { System.out.println(“您想要多大的比萨饼?”); 做 { System.out.print(“请输入比萨饼的长度(小于60英寸:); lengthOfPizza=keyboard.nextDouble(); 如果(lengthOfPizza>60&&lengthOfPizza60&&lengthOfPizza60和比扎宽度60和比扎宽度
}if语句中的初始化变量,稍后在代码中使用,但表示未定义 import java.util.Scanner; 导入java.lang.Math; 公共类比萨饼 { 公共静态void main(字符串[]args) { 双倍形; 双层浇头; 双层披萨皮; 双基成本; 双倍面积; 两倍的浇头; 一个顶部的最终双倍成本=0.025; 面团的最终双倍成本=0.019; 酱汁的最终双倍成本=0.036; 双倍直径的比萨; 双倍长度; 双倍宽的比萨; 双倍容积; 最后的双薄和脆=0.1; 最终双盘=0.5; 最终双经典手抛=0.25; 最后的双德州土司=0.9; 双粒锈; 双干酪锈菌; 材料的最终双重成本=0.02; 双份比萨饼; 交付成本加倍; 双重征税; 双层比萨饼; 最终双PI=3.14159; 双壳型; 双倍成本; 双倍的生产成本; 扫描仪键盘=新扫描仪(System.in); System.out.println(“你好,客户!欢迎来到Guiseppi's Just Pizza!”); System.out.println(“我们为您制作比萨饼的地方!”); System.out.println(“\n您想要什么样的比萨饼”+ “今天天气真好?”; 做 { System.out.print(“按1表示方形比萨饼,”); System.out.print(“或按2表示圆形比萨饼:”); shapeOfPizza=keyboard.nextDouble(); if(shapeoppizza!=1&&shapeoppizza!=2) { System.out.println(“输入不正确,请输入1或2”); } } while(shapeOfPizza!=1&&shapeOfPizza!=2); if(shapeOfPizza==1) { System.out.println(“您想要多大的比萨饼?”); 做 { System.out.print(“请输入比萨饼的长度(小于60英寸:); lengthOfPizza=keyboard.nextDouble(); 如果(lengthOfPizza>60&&lengthOfPizza60&&lengthOfPizza60和比扎宽度60和比扎宽度,java,variables,if-statement,initialization,Java,Variables,If Statement,Initialization,} 因此,当我试图编译这个时,变量numberOfTopping、areaOfPizza和sizeOfCrust都是错误的原因,因为“它们没有初始化”,但我已经将它们都定义为双精度,并将numberOfTopping设置为键盘。下一步,如果有其他两个公式,我们将不胜感激。编译器是正确的 numberOfToppings在标题中定义,但只有当toppings==1时,才会在进入循环后分配一个值 稍后,当您计算基本成本时使用numberOfToppings的值,此时在代码中,用户可以选择不使用任何t
因此,当我试图编译这个时,变量numberOfTopping、areaOfPizza和sizeOfCrust都是错误的原因,因为“它们没有初始化”,但我已经将它们都定义为双精度,并将numberOfTopping设置为键盘。下一步,如果有其他两个公式,我们将不胜感激。编译器是正确的
numberOfToppings
在标题中定义,但只有当toppings==1时,才会在进入循环后分配一个值
稍后,当您计算基本成本时
使用numberOfToppings
的值,此时在代码中,用户可以选择不使用任何toppings,并且从未为numberOfToppings
分配初始值
为了避免这种情况,您应该在标头中将变量初始化为所需的默认值0
import java.util.Scanner;
import java.lang.Math;
public class pizzasMazboudi
{
public static void main(String[] args)
{
double shapeOfPizza;
double toppings;
double pizzaCrust;
double baseCost;
double areaOfPizza;
double numberOfToppings;
final double COST_OF_ONE_TOPPING = 0.025;
final double COST_OF_DOUGH = 0.019;
final double COST_OF_SAUCE = 0.036;
double diameterOfPizza;
double lengthOfPizza;
double widthOfPizza;
double volumeOfDough;
final double THIN_AND_CRISPY = .1;
final double PAN = .5;
final double CLASSIC_HAND_TOSSED = .25;
final double TEXAS_TOAST = .9;
double sizeOfCrust;
double cheesyCrust;
final double COST_OF_MATERIALS = .02;
double numberOfPizzas;
double costOfDelivery;
double tax;
double pizzaDelivery;
final double PI = 3.14159;
double typeOfCrust;
double costOfCheesyCrust;
double costOfPizzaDelivery;
Scanner keyboard = new Scanner(System.in);
System.out.println("Hello customer! Welcome to Guiseppi's Just Pizza!");
System.out.println("Where we make the pizza just for you!");
System.out.println("\n What kind of pizza do you want on" +
" this beautiful day?");
do
{
System.out.print("Press 1 for a square pizza,");
System.out.print(" or press 2 for a circle pizza: ");
shapeOfPizza = keyboard.nextDouble();
if(shapeOfPizza != 1 && shapeOfPizza != 2)
{
System.out.println("That is an incorrect input, please enter 1 or 2");
}
}
while(shapeOfPizza != 1 && shapeOfPizza != 2);
if(shapeOfPizza == 1)
{
System.out.println("And how big would you like your pizza?");
do
{
System.out.print("Please enter the length of your pizza (under 60 inches: ");
lengthOfPizza = keyboard.nextDouble();
if(lengthOfPizza > 60 && lengthOfPizza < 0)
{
System.out.println("That is an incorrect input, please enter a number between 0 and 60");
}
}
while(lengthOfPizza > 60 && lengthOfPizza < 0);
do
{
System.out.print("Please enter the width of your pizza: ");
widthOfPizza = keyboard.nextDouble();
if(widthOfPizza >60 && widthOfPizza <0)
{
System.out.println("That is an incorrect input, please enter a number between 0 and 60.");
}
}
while(widthOfPizza > 60 && widthOfPizza < 0);
areaOfPizza = lengthOfPizza * widthOfPizza;
System.out.println("Your pizza is " + areaOfPizza + "inches large");
sizeOfCrust = 2 * (lengthOfPizza + widthOfPizza);
}
else if(shapeOfPizza == 2)
{
System.out.println("And how big would you like your pizza?");
do
{
System.out.print("Please enter the diameter of your pizza: ");
diameterOfPizza = keyboard.nextDouble();
if(diameterOfPizza > 60 && diameterOfPizza <= 0)
{
System.out.println("That is an incorrect input, please enter a number that is between 0 and 60.");
}
}
while(diameterOfPizza > 60 && diameterOfPizza <= 0);
sizeOfCrust = 2*PI*(diameterOfPizza/2);
areaOfPizza = PI*((diameterOfPizza/2)*(diameterOfPizza/2));
System.out.println("The area of your pizza is" + areaOfPizza + " inches large");
}
System.out.println("What type of crust would you like?");
do
{
System.out.println("Enter 1 for Thin and Crispy, 2 for Pan,");
System.out.println("3 for Classic Hand-Tossed, and 4 for Texas Toast");
pizzaCrust = keyboard.nextDouble();
if(pizzaCrust != 2 && pizzaCrust != 3 && pizzaCrust != 4)
{
System.out.println("That is an incorrect input, please enter 1, 2, 3, or 4");
}
}
while(pizzaCrust != 2 && pizzaCrust != 3 && pizzaCrust != 4);
if(pizzaCrust == 1);
{
typeOfCrust = THIN_AND_CRISPY;
}
if(pizzaCrust == 2);
{
typeOfCrust = PAN;
}
if(pizzaCrust == 3);
{
typeOfCrust = CLASSIC_HAND_TOSSED;
}
if(pizzaCrust == 4);
{
typeOfCrust = TEXAS_TOAST;
}
if(pizzaCrust == 2 && pizzaCrust == 3 && pizzaCrust == 4)
{
System.out.println("Would you like to try our new cheesy crust today?");
do
{
System.out.print("Enter 1 for yes or 2 for no");
cheesyCrust = keyboard.nextDouble();
if(cheesyCrust != 1 && cheesyCrust != 2)
{
System.out.println("That is an incorrect input, please enter 1 or 2");
}
}
while(cheesyCrust != 1 && cheesyCrust != 2);
}
System.out.println("Would you like any toppings today?");
do
{
System.out.println("Enter 1 for toppings, enter 2 if you do not want toppings");
toppings = keyboard.nextDouble();
if(toppings != 1 && toppings !=2)
{
System.out.println("That is an incorrect input, please enter 1 or 2");
}
}
while(toppings != 1 && toppings !=2);
if(toppings == 1)
{
do
{
System.out.println("How many toppings do you want? (10 toppings limit)");
numberOfToppings = keyboard.nextDouble();
if(numberOfToppings <=0 && numberOfToppings > 10)
{
System.out.println("That is an incorrect input, please enter a number 1 through ten");
}
}
while(numberOfToppings <=0 && numberOfToppings > 10);
}
do
{
System.out.println("How many identical pizza's would you like?");
numberOfPizzas = keyboard.nextDouble();
if(numberOfPizzas <= 0)
{
System.out.println("That is an incorrect input, please enter a number above 0");
}
}
while(numberOfPizzas <= 0);
System.out.println("Will you come pick up your order, or will you like delivery?");
do
{
System.out.println("Enter 1 for delivery, enter 2 for pick-up");
pizzaDelivery = keyboard.nextInt();
if(pizzaDelivery != 1 && pizzaDelivery != 2)
{
System.out.println("That is an incorrect input, please enter a 1 or 2");
}
}
while(pizzaDelivery != 1 && pizzaDelivery != 2);
costOfCheesyCrust = sizeOfCrust * COST_OF_MATERIALS;
volumeOfDough = typeOfCrust * areaOfPizza;
baseCost = areaOfPizza * (COST_OF_SAUCE + numberOfToppings * COST_OF_ONE_TOPPING) + COST_OF_DOUGH * volumeOfDough;
if(cheesyCrust == 1)
{
costOfCheesyCrust = sizeOfCrust * COST_OF_MATERIALS;
baseCost = baseCost + costOfCheesyCrust;
}
if(pizzaDelivery == 1)
{
if(baseCost < 10)
{
costOfPizzaDelivery = 3.00;
}
else if(baseCost < 20 || baseCost >= 10)
{
costOfPizzaDelivery = 2.00;
}
else if(baseCost < 30 || baseCost >= 20)
{
costOfPizzaDelivery = 1.00;
}
else if(baseCost >= 30)
{
costOfPizzaDelivery = 0;
}
}
}
同样的道理也适用于你提到的其他变量,都是出于同样的原因。根据用户输入,这些变量可能永远不会被初始化,但你仍然尝试在最终计算中使用它们;你应该全部初始化它们
作为旁注,您应该学习在IDE中使用自动格式化程序以提高代码可读性。在声明这些变量时,您应该使用一些默认值初始化它们
您的代码很混乱,但这些变量永远无法根据用户输入进行设置,因此基本上不能使用未初始化的变量
作为一种最佳做法,在使用前不要声明局部变量,这可能会增加它们的作用域。当需要使用变量时,应立即声明并初始化它。只需初始化它们……在声明它们时输入它们的默认值。double shapeOfPizza=0.0;
对所有其他局部变量执行此操作E
double numberOfToppings = 0;