gpt4 book ai didi

java - 为什么值没有正确增加?

转载 作者:行者123 更新时间:2023-12-02 04:20:29 24 4
gpt4 key购买 nike

我正在开发一个 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 $10.00 each?");
numPlain = kbd.nextInt();
System.out.println("How many pepperoni pizzas would you like for $12.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 - 为什么值没有正确增加?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32831488/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com