我是一名新的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;
}
}
答案 0 :(得分:3)
一旦您进入while (true)
大小为“错误”的循环,您就不会做任何更改大小的操作,因此您永远不会脱离循环。
答案 1 :(得分:0)
我评论了您的一些代码,并用更简单的方法重写了它们。
您知道每个循环吗?它更容易循环,非常适合您的程序。 如果您不了解,请告诉我,
我做了什么:
Ex-ArrayList stringArray = new ArrayList <>();
因此,在您的情况下,比萨对象[pizza1,pizza2,pizza3]的ArrayList
我的观察:
您的目标-“我希望它继续要求用户输入尺寸”
为此而不是将while(true)设置为布尔值
boolean status = true; while(状态){ 如果(){ 状态=假; //而不是休息;将状态设为假 }其他{ status = true //由于条件为,因此while循环将迭代 true}
如果有任何问题请回复我,欢呼声
import java.util.ArrayList;
import java.util.Scanner;
public class Pizza2Test {
private static Scanner userInput; //declare scanner
private static ArrayList<Pizza2> pizzaList = new ArrayList<>();
static int lQuantity;
// 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
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 < lQuantity; 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("Sorry you entered some invalid values, Please
try again");
System.exit(0);
}
// 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();
myPizza.setNumberOfToppings(numberOfToppings);
// 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 < numberOfToppings; 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
// myToppings.add(i, userInput.next());
myPizza.setToppings(userInput.next());
}
// myPizza.setToppings(myToppings);
// myPizza.setToppings(myToppings);
// pizzaList [cnt] = myPizza;
pizzaList.add(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.size());//get quantity from
stored value
for (int j = 0; j < pizzaList.size(); 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("Size: " + pizzaList.get(j).getSize());
// System.out.println("Number Of Toppings: " +
pizzaList[j].getNumberOfToppings());//get number of toppings from stored value
System.out.println("Number of Toppings: " +
pizzaList.get(j).getNumberOfToppings());
// String [] myToppings = pizzaList[j].getToppings();//get toppings from
stored value
ArrayList myToppings2 = pizzaList.get(j).getToppings();
for (int i = 0; i < myToppings2.size(); i++) {
// System.out.println("Toppings: [" + (i+1) + "]: " + myToppings[i]);
System.out.println("Toppings: [" + (i + 1) + "]: " +
myToppings2.get(i));
}
}
}
}
和比萨饼对象类
import java.util.ArrayList;
public class Pizza2
{
private int size;
// private String [] toppings;
private ArrayList<String>toppings=new ArrayList<>();
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;
// }
public void setNumberOfToppings (int numberOfToppings)
{
// numberOfToppings = getToppings().length;
this.numberOfToppings=numberOfToppings;
}
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;
}
/**
* @return the toppings
*/
public ArrayList<String> getToppings() {
return toppings;
}
/**
* @param toppings the toppings to set
*/
public void setToppings(String topping) {
this.toppings.add(topping);
}
}