为什么一个值没有正确递增?
Why is a value not incrementing properly?
我正在做一个 Java 项目,遇到了一个奇怪的错误。我有一个包含多个选项的菜单,所有选项都在一个 do while 循环中,但我希望变量 pizzasOrdered 在用户向他的订单中添加更多比萨饼时保持增加。这是我的代码:
import java.util.Scanner;
public class PizzaMenu {
public static void main(String[] args) {
int numPlain;
int numPepperoni;
int pizzasOrdered = 0;
boolean flag = true;
System.out.println("Welcome to Pies, Pies, and pis!");
Scanner kbd = new Scanner(System.in);
System.out.println("Is there a customer in line? (1 = yes, 2 = no)");
int isCustomer = kbd.nextInt();
System.out.println("Are you a Pie Card member? (1 = yes, 2 = no)");
boolean isMember;
int pieCardResponse = kbd.nextInt();
if(pieCardResponse == 1)
{
isMember = true;
}
else
{
isMember = false;
}
do{
System.out.println("Please choose an option: \n\t1) Update Pizza Order\n\t2) Update Cherry Pie Order\n\t3) Update Charm Order\n\t4) Check Out");
int option = kbd.nextInt();
if(option == 1)
{
System.out.println("Here is your current order: \n\t" + pizzasOrdered + " pizzas ordered");
System.out.println("How many plain pizzas would you like for .00 each?");
numPlain = kbd.nextInt();
System.out.println("How many pepperoni pizzas would you like for .00 each?");
numPepperoni = kbd.nextInt();
pizzasOrdered = numPlain + numPepperoni;
}
else if(option == 2)
{
System.out.println("Here is your current order: \n");
}
else if(option == 3)
{
}
else if(option == 4)
{
}
}while(flag);
}
}
你应该改变这个
pizzasOrdered = numPlain + numPepperoni;
至
pizzasOrdered += (numPlain + numPepperoni);
在您的情况下,您没有递增变量,只是在每次迭代中为其分配新值。
我正在做一个 Java 项目,遇到了一个奇怪的错误。我有一个包含多个选项的菜单,所有选项都在一个 do while 循环中,但我希望变量 pizzasOrdered 在用户向他的订单中添加更多比萨饼时保持增加。这是我的代码:
import java.util.Scanner;
public class PizzaMenu {
public static void main(String[] args) {
int numPlain;
int numPepperoni;
int pizzasOrdered = 0;
boolean flag = true;
System.out.println("Welcome to Pies, Pies, and pis!");
Scanner kbd = new Scanner(System.in);
System.out.println("Is there a customer in line? (1 = yes, 2 = no)");
int isCustomer = kbd.nextInt();
System.out.println("Are you a Pie Card member? (1 = yes, 2 = no)");
boolean isMember;
int pieCardResponse = kbd.nextInt();
if(pieCardResponse == 1)
{
isMember = true;
}
else
{
isMember = false;
}
do{
System.out.println("Please choose an option: \n\t1) Update Pizza Order\n\t2) Update Cherry Pie Order\n\t3) Update Charm Order\n\t4) Check Out");
int option = kbd.nextInt();
if(option == 1)
{
System.out.println("Here is your current order: \n\t" + pizzasOrdered + " pizzas ordered");
System.out.println("How many plain pizzas would you like for .00 each?");
numPlain = kbd.nextInt();
System.out.println("How many pepperoni pizzas would you like for .00 each?");
numPepperoni = kbd.nextInt();
pizzasOrdered = numPlain + numPepperoni;
}
else if(option == 2)
{
System.out.println("Here is your current order: \n");
}
else if(option == 3)
{
}
else if(option == 4)
{
}
}while(flag);
}
}
你应该改变这个
pizzasOrdered = numPlain + numPepperoni;
至
pizzasOrdered += (numPlain + numPepperoni);
在您的情况下,您没有递增变量,只是在每次迭代中为其分配新值。