作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是一名新的 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/
我有两种方法(在 C# 中): List CookPizza(List); List DeliverPizza(List); 这些操作没有共同的对象(除了从一个传递到另一个的比萨饼),并且是线程安全的
我是一名优秀的程序员,十分优秀!