我有兩個類:比薩和PizzaOrder。 PizzaOrder允許用戶按照他的順序創建1-3個比薩餅的新訂單。輸入比薩餅的數量後,它會繼續輸入每個披薩的信息。但是我有Scanner出錯,輸入比薩後我再也輸入不了了。無法輸入掃描儀線
這裏是我的代碼:
類比薩:
public class Pizza {
public static void main(String[] args){
Pizza pizza1 = new Pizza("Large", 1, 1, 2);
Pizza pizza2 = new Pizza("Small", 3, 0, 1);
Pizza pizza3 = new Pizza("Medium", 1, 1, 3);
System.out.println(pizza1.getDescription());
System.out.println(pizza2.getDescription());
System.out.println(pizza3.getDescription());
}
private String size;
private int numberOfCheese, numberOfPepperoni, numberOfHam;
public Pizza(String size, int numberOfCheese, int numberOfPepperoni,
int numberOfHam) {
super();
this.size = size;
this.numberOfCheese = numberOfCheese;
this.numberOfPepperoni = numberOfPepperoni;
this.numberOfHam = numberOfHam;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
public int getNumberOfCheese() {
return numberOfCheese;
}
public void setNumberOfCheese(int numberOfCheese) {
this.numberOfCheese = numberOfCheese;
}
public int getNumberOfPepperoni() {
return numberOfPepperoni;
}
public void setNumberOfPepperoni(int numberOfPepperoni) {
this.numberOfPepperoni = numberOfPepperoni;
}
public int getNumberOfHam() {
return numberOfHam;
}
public void setNumberOfHam(int numberOfHam) {
this.numberOfHam = numberOfHam;
}
public double calcCost(){
if(size.equals("Small"))
return 10 + numberOfCheese*2 + numberOfPepperoni*2 + numberOfHam*2;
else if(size.equals("Medium"))
return 12 + numberOfCheese*2 + numberOfPepperoni*2 + numberOfHam*2;
return 14 + numberOfCheese*2 + numberOfPepperoni*2 + numberOfHam*2;
}
public String getDescription(){
return "Pizza size: "+size +
"\nNumber Of Cheese Topping: " + getNumberOfCheese()
+"\nNumber Of Pepperoni Topping: "+ getNumberOfPepperoni()
+"\nNumber Of Ham Topping: "+ getNumberOfHam()
+"\nPizza cost: " + calcCost()+"$\n";
}
}
類PizzaOrder:
import java.util.Random;
import java.util.Scanner;
public class PizzaOrder {
public static void main(String[] args){
PizzaOrder pizzaOrder = new PizzaOrder();
pizzaOrder.setNumPizzas(pizzaOrder.numPizzas);
}
private Pizza pizza1, pizza2, pizza3;
private int numPizzas;
private int id;
public PizzaOrder(){
System.out.println("Enter the number of Pizza: ");
Scanner scanner = new Scanner(System.in);
this.numPizzas = scanner.nextInt();
scanner.close();
Random random = new Random();
int range = 100 - 0 + 1;
this.id = random.nextInt(range) + 0;
}
public void setNumPizzas(int numPizzas){
if(numPizzas == 1){
System.out.println("-----------Enter Pizza1's Information-----------");
enterInformation(pizza1);
}
else if(numPizzas == 2){
System.out.println("-----------Enter Pizza1's Information-----------");
enterInformation(pizza1);
System.out.println("-----------Enter Pizza2's Information-----------");
enterInformation(pizza2);
}
else if(numPizzas == 3){
System.out.println("-----------Enter Pizza1's Information-----------");
enterInformation(pizza1);
System.out.println("-----------Enter Pizza2's Information-----------");
enterInformation(pizza2);
System.out.println("-----------Enter Pizza3's Information-----------");
enterInformation(pizza3);
}
}
public double calcTotal(){
return pizza1.calcCost() + pizza2.calcCost() + pizza3.calcCost();
}
public void enterInformation(Pizza pizza){
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the pizza's size: ");
String size = scanner.nextLine();
pizza.setSize(size);
System.out.println("Enter the number of Cheese topping: ");
int numberOfCheese = scanner.nextInt();
pizza.setNumberOfCheese(numberOfCheese);
System.out.println("Enter the number of Pepperoni topping: ");
int numberOfPepperoni = scanner.nextInt();
pizza.setNumberOfPepperoni(numberOfPepperoni);
System.out.println("Enter the number of Ham topping: ");
int numberOfHam = scanner.nextInt();
pizza.setNumberOfHam(numberOfHam);
scanner.close();
System.out.println("This pizza cost: " + pizza.calcCost() +"$");
}
}
當我輸入PizzaOrder類比薩== 1的數字,它顯示錯誤:
Enter the number of Pizza:
1
-----------Enter Pizza1's Information-----------
Exception in thread "main" java.lang.NullPointerException
at bai2.PizzaOrder.enterInformation(PizzaOrder.java:69)
at bai2.PizzaOrder.setNumPizzas(PizzaOrder.java:30)
at bai2.PizzaOrder.main(PizzaOrder.java:10)
如何解決這個問題?
雖然這不是問題,如果你的Pizza類沒有擴展任何類,爲什麼要調用'super()'?檢查[不正確的做法是調用super()的類不擴展任何東西?](http://stackoverflow.com/questions/4679314/is-it-bad-practice-to-call-super-in -a-class-that-doesnt-extend-anything) – sam
你還沒有初始化'pizza1'。 – dejvuth
您可以突出顯示PizzaOrder.java中第69行的代碼行: – Pavan