2015-11-02 135 views
-1

我有兩個類:比薩和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) 

如何解決這個問題?

+0

雖然這不是問題,如果你的Pizza類沒有擴展任何類,爲什麼要調用'super()'?檢查[不正確的做法是調用super()的類不擴展任何東西?](http://stackoverflow.com/questions/4679314/is-it-bad-practice-to-call-super-in -a-class-that-doesnt-extend-anything) – sam

+0

你還沒有初始化'pizza1'。 – dejvuth

+0

您可以突出顯示PizzaOrder.java中第69行的代碼行: – Pavan

回答

1

,必須先創建Pizza實例,然後聲明如果您訪問它的成員函數一樣pizza.setSizeenterInformation

在每一個;

pizza1 = new Pizza() 
System.out.println("-----------Enter Pizza1's Information-----------"); 
enterInformation(pizza1); 

你也已經pizzaOrder.numPizzassetNumPizzas如此反覆傳遞是多餘的;我建議你使用多個Pizza實例的數組。

+0

我和你一樣,但它顯示錯誤: 輸入比薩的數量: -----------輸入Pizza1的信息-----------例外線程 「主要」 輸入比薩的尺寸: java.util.NoSuchElementException:沒有找到行 \t在java.util.Scanner.nextLine(Scanner.java:1585) \t在bai2.PizzaOrder.enterInformation(PizzaOrder.java: 56) \t在bai2.PizzaOrder.setNumPizzas(PizzaOrder.java:31) \t在bai2.PizzaOrder.main(PizzaOrder.java:10) – Khuong

+0

首先檢查(scanner.hasNextLine()),然後調用nextLine –

0

不應該使用掃描儀的多個實例包裝輸入流System.in,因爲一旦你關閉它,它不應該再被使用。

用System.in聲明一個Scanner的單個實例。您可以聲明一個持有掃描程序全局實例的Utils類。或者只是將一個Scanner的公共靜態實例放入Pizza或PizzaOrder中。 - 只要它可以在全球範圍內訪問,任何一種方式都可以。

public InputUtil { 
    public static Scanner scanner = new Scanner(System.in); 
} 

用作

InputUtil.scanner.nextInt(); 
+0

但我應該在哪裏申報? – Khuong

+0

@ khuong291您可以創建一個實用程序類,它只有一個掃描儀的公共靜態實例 – Bon

0

隨着未初始化pizza1,2,3的情況下,問題是,你從setNumPizza關閉流

這裏

public static void main(String[] args){ 
    PizzaOrder pizzaOrder = new PizzaOrder(); 
    pizzaOrder.setNumPizzas(pizzaOrder.numPizzas); 
} 

public PizzaOrder(){ 
    ... 
    scanner.close(); 
    ... 
} 

然後其中呼叫enterInformation

public void enterInformation(Pizza pizza){ 
    Scanner scanner = new Scanner(System.in); 
    ... 
} 

一旦您調用scanner.close,它將關閉基礎流。創建一個實例

+0

我明白你在這裏表達什麼。但是Scanner的新實例不會重新打開底層流並且工作得很好嗎?爲什麼它不這樣做? – Pavan

+0

但我應該在哪裏申報? – Khuong

+0

@Pavan檢查[這裏](http://stackoverflow.com/questions/17622556/scanner-method-opened-and-closed-twice) – sam

-1

我固定它通過scanner.nextLine()

這是我在PizzaOrder類代碼替換scanner.close()

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); 
     pizzaOrder.getPizzaOrderDescription(); 
    } 

    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.nextLine(); 
     Random random = new Random(); 
     int range = 100 - 0 + 1; 
     this.id = random.nextInt(range) + 0; 
    } 

    public void setNumPizzas(int numPizzas){ 
     if(numPizzas == 1){ 
      pizza1 = new Pizza("", 1, 1, 1); 
      System.out.println("-----------Enter Pizza1's Information-----------"); 
      enterInformation(pizza1); 
     } 
     else if(numPizzas == 2){ 
      pizza1 = new Pizza("", 1, 1, 1); 
      System.out.println("-----------Enter Pizza1's Information-----------"); 
      enterInformation(pizza1); 
      pizza2 = new Pizza("", 1, 1, 1); 
      System.out.println("-----------Enter Pizza2's Information-----------"); 
      enterInformation(pizza2); 
     } 
     else if(numPizzas == 3){ 
      pizza1 = new Pizza("", 1, 1, 1); 
      System.out.println("-----------Enter Pizza1's Information-----------"); 
      enterInformation(pizza1); 
      pizza2 = new Pizza("", 1, 1, 1); 
      System.out.println("-----------Enter Pizza2's Information-----------"); 
      enterInformation(pizza2); 
      pizza3 = new Pizza("", 1, 1, 1); 
      System.out.println("-----------Enter Pizza3's Information-----------"); 
      enterInformation(pizza3); 
     } 
    } 

    public double calcTotal(){ 
     if(numPizzas == 1) 
      return pizza1.calcCost(); 
     else if(numPizzas == 2) 
      return pizza1.calcCost() + pizza2.calcCost(); 
     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.nextLine(); 
     System.out.println("This pizza cost: " + pizza.calcCost() +"$"); 
    } 

    public void getPizzaOrderDescription(){ 
     System.out.println("Your total cost is: " + calcTotal() +"$"); 
    } 
} 

輸出:

Enter the number of Pizza: 
3 
-----------Enter Pizza1's Information----------- 
Enter the pizza's size: 
Large 
Enter the number of Cheese topping: 
1 
Enter the number of Pepperoni topping: 
1 
Enter the number of Ham topping: 
1 
This pizza cost: 20.0$ 
-----------Enter Pizza2's Information----------- 
Enter the pizza's size: 
Large 
Enter the number of Cheese topping: 
1 
Enter the number of Pepperoni topping: 
1 
Enter the number of Ham topping: 
1 
This pizza cost: 20.0$ 
-----------Enter Pizza3's Information----------- 
Enter the pizza's size: 
Small 
Enter the number of Cheese topping: 
2 
Enter the number of Pepperoni topping: 
2 
Enter the number of Ham topping: 
2 
This pizza cost: 22.0$ 
Your total cost is: 62.0$