gpt4 book ai didi

Java - While Loop不断重复披萨程序

转载 作者:行者123 更新时间:2023-11-29 04:13:11 26 4
gpt4 key购买 nike

我是一名新的 Java 程序员,我正在编写一个披萨程序,我试图在其中设置和获取披萨的大小,但是如果用户输入了错误的值,我就会陷入循环。我希望它继续要求用户输入尺寸。如果我还可以在输出总价方面获得帮助,那么一旦我获得尺寸并从 Pizza2 类获得价格,那将是很棒的,但如果不是,我只是试图至少解决这个问题,因为尺寸卡在了循环。

package hello;

import java.util.Scanner;

public class Pizza2Test
{
private static Scanner userInput; //declare scanner

private static Pizza2 [] pizzaList; //declare pizzaList array

public static void main (String [] args)
{
userInput = new Scanner(System.in); //initalize scanner

System.out.print("Enter quantity of pizzas: "); //ask user to enter quantity of pizza
int lQuantity = userInput.nextInt(); //store users input
pizzaList = new Pizza2[lQuantity]; //creating a pizza list with quantity being the size of the array

populatePizzaList();
displayPizzaList();

userInput.close(); //close scanner
}
//populate the array with pizza information
//
private static void populatePizzaList ()
{
int numberOfToppings = 0;
//System.out.println("Length of Array: " + pizzaList.length);
for (int cnt = 0; cnt < pizzaList.length; cnt++) //start at 0 and increment and loop until 1 less than pizzaLists length
{
Pizza2 myPizza = new Pizza2(); //call Pizza2 class
System.out.print("Enter size of pizzas (enter '0' for Large or '1' for Medium or '2' for Small): "); //ask user to enter size
myPizza.setSize(userInput.nextInt()); //store size from pizza2 class
while(true)
{
if(myPizza.getSize() == 0)
{
myPizza.getLARGE();
break;
}
else if (myPizza.getSize() == 1)
{
myPizza.getMEDIUM();
break;
}
else if (myPizza.getSize() == 2)
{
myPizza.getSMALL();
break;
}
else
{
System.out.println("Please enter '0' for Large or '1' for Medium or '2' for Small. ");
}
}
System.out.print("Enter number of toppings of pizzas: "); //ask user to enter number of toppings
numberOfToppings = userInput.nextInt();
String [] myToppings = new String[numberOfToppings];
//myPizza.setNumberOfToppings(userInput.nextInt());//store user number of toppings
//String [] myToppings = new String[myPizza.getNumberOfToppings()];
//int pNumberOfToppings = myPizza.getNumberOfToppings();
for (int i = 0; i < myToppings.length ; i++) //start at 0 and increment until number of toppings
{
System.out.print("Enter toppings: ");//ask user to enter toppings
myToppings[i] = userInput.next();//store user toppings
}
myPizza.setToppings(myToppings);
pizzaList [cnt] = myPizza;
}
}
//display the array with pizza information from the pizza array list
//
private static void displayPizzaList ()//number of pizza information to display
{
System.out.println("Quantity: " + pizzaList.length);//get quantity from stored value
for (int j = 0; j < pizzaList.length; j++) //start at 0 and loop until pizza list length
{
System.out.println("Size: " + pizzaList[j].getSize());//get size from stored value
System.out.println("Number Of Toppings: " + pizzaList[j].getNumberOfToppings());//get number of toppings from stored value
String [] myToppings = pizzaList[j].getToppings();//get toppings from stored value
for (int i = 0; i < myToppings.length; i++)
{
System.out.println("Toppings: [" + (i+1) + "]: " + myToppings[i]);
}
}
}
}

package hello;

public class Pizza2
{
private int size;
private String [] toppings;
private int numberOfToppings;
private double price;
private final double LARGE = 13.99;
private final double MEDIUM = 10.99;
private final double SMALL = 7.99;
private final double TOPPINGS = 0.50;

public Pizza2()
{

}
public void setSize (int pSize)
{
size = pSize;
}

public int getSize()
{
return size;
}
public void setToppings (String [] pToppings)
{
toppings = pToppings;
}

public String [] getToppings()
{
return toppings;
}
private void setNumberOfToppings ()
{
numberOfToppings = toppings.length;
}

public int getNumberOfToppings()
{
return numberOfToppings;
}
public void setPrice (double pPrice)
{
price = pPrice;
}
public double getPrice()
{
// if(size )
return price;
}
public double getLARGE() {
return LARGE;
}
public double getMEDIUM() {
return MEDIUM;
}
public double getSMALL() {
return SMALL;
}
public double getTOPPINGS() {
return TOPPINGS;
}
}

最佳答案

一旦您以“错误”大小进入 while (true) 循环,您不会做任何改变大小的事情,因此您永远不会跳出循环。

关于Java - While Loop不断重复披萨程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53891993/

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