2016-06-21 24 views
3

我正在練習製作烹飪課程,即時貼上廚師烹飪餐食,以及如何計算兩位廚師烹飪的餐食數量。廚師做飯和如何回去?

import java.util.Scanner; 

public class Cooking { 

int count = 0; 
String chefName; 
String foodName; 
Scanner scanner = new Scanner(System.in); 

public void chefName() { 
    System.out.println("What is the name of chef 1?"); 
    scanner.next(); 
    System.out.println("What is the name of chef 2?"); 
    scanner.next(); 
    count++; 
    //System.out.println("This week you made " + count + " deliveries!"); 
    //System.out.println("Bye, " + driverName + "!"); 
} 

    public void foodName() { 
    Scanner console = new Scanner(System.in); 
    System.out.println("Meal 1: What would you like to eat?"); 
    foodName = scanner.nextLine(); 
    System.out.println(chefName + " is cooking " + foodName); 
    System.out.println("Meal 2: What would you like to eat?"); 
    foodName = scanner.nextLine(); 
    System.out.println(chefName + " is cooking " + foodName); 
    scanner.next(); 
    count++; 
    } 
} 

public class MainProgram1 { 

public static void main(String[] args) { 
    // Create cooking records 
    Cooking cookingByBob = new Cooking(); 
    cookingByBob.chefName = "Bob"; 

    Cooking cookingByAnne = new Cooking(); 
    cookingByAnne.chefName = "Anne"; 

    // Start delivering 
    System.out.println("Hi!"); 
    cookingByBob.chefName(); 
    cookingByAnne.foodName(); 

當我運行它時,安妮是烹飪所有飯菜的人。

我如何做到這樣安妮做飯1然後鮑勃2和3和安妮4 5?我又如何計算所有的熟食?

+0

在你mainProgram1類,我可以看到你和安妮對象調用食品名稱方法烹飪班只有一次。 – sathya

+1

另外,如果您可以恰當地設定您的問題,請按照要執行的操作以及按照什麼順序打印它,這將會很有幫助。 – sathya

回答

2

你的主要問題是你得到了很多錯誤的抽象;這使得你很難將這些東西添加到你真正需要的代碼中。例如:類應該代表一些「真實」的概念;與現實相似的東西。你的班級Cooking不是這樣的。首先是這個班級向用戶詢問廚師和食物的名稱;但另一方面,您的主程序以編程方式之後爲這些字段分配值。這只是非常混亂。

只是考慮改變這樣整個事情:

class Cook { 
    private final String name; 
    Cook(String name) { this.name = name; } 

允許你創建

Cook bob = new Cook("bob"); 

然後,你可以添加方法,以煮像

class Cook { ... 
    void prepareFood(String foodName) ... 
    List<String> getPreparedFoods() ... 

,現在你可以做

bob.prepareFood("1"); 
anne.prepareFood("2"); 

,後來

System.out.println("food by bob: " + bob.getPreparedFoods()); 

什麼的一致好評。問題是:你當前的程序中有很多「噪音」,這使得你很難以正確的方式寫下這個簡單的序列。長話短說:在你開始編碼之前,考慮你想要實現的「故事」所需的「事物」和「行爲」。然後圍繞這些抽象建立你的程序。

1

這應該是一個使用您熟悉的工具的工作解決方案,即使讀取代碼表明您缺乏對某些基本Java概念的理解。不要只複製粘貼這個,嘗試修改你自己的代碼。首先,您可以刪除所有過時的行。

public class Kitchen { 

    int count = 0; 
    String firstChefName; 
    String secondChefName; 
    String foodName;  
    Scanner scanner = new Scanner(System.in); 

    public void setChefName() { 
     System.out.println("What is the name of chef 1?"); 
     firstChefName = scanner.next(); 
     System.out.println("What is the name of chef 2?"); 
     secondChefName = scanner.next(); 
    } 

    public void setFoodName() { 
     System.out.println("Meal 1: What would you like to eat?"); 
     foodName = scanner.nextLine(); 
     System.out.println(firstChefName + " is cooking " + foodName); 
     count++; 

     System.out.println("Meal 2: What would you like to eat?"); 
     foodName = scanner.nextLine(); 
     System.out.println(secondChefName + " is cooking " + foodName); 
     count++; 

     System.out.println("Meal 3: What would you like to eat?"); 
     foodName = scanner.nextLine(); 
     System.out.println(secondChefName + " is cooking " + foodName); 
     count++; 

     System.out.println("Meal 4: What would you like to eat?"); 
     foodName = scanner.nextLine(); 
     System.out.println(firstChefName + " is cooking " + foodName); 
     count++; 

     System.out.println("Meal 5: What would you like to eat?"); 
     foodName = scanner.nextLine(); 
     System.out.println(firstChefName + " is cooking " + foodName); 
     count++; 
    } 

} 

主要方法:

public static void main(String[] args) { 

    // Create cooking records 
    Kitchen kitchen = new Kitchen(); 

    // Start delivering 
    System.out.println("Hi!"); 
    kitchen.setChefName(); 
    kitchen.setFoodName(); 
} 
+0

我沒有跑過你的代碼,但不會在'kitchen.setChefName()之後運行;'它只會打印我們的餐1:你會......?暫停首先​​讓用戶輸入迴應? –

+0

不,因爲setChefName()被調用,方法中的每行代碼都會在它的setFoodName()之前執行。掃描器。next()是一種所謂的阻塞方法,用戶需要輸入代碼才能繼續。 – Raijen

+0

嘗試使用調試器,在每個關鍵線上放置一個斷點並查看執行順序。這應該會進一步幫助你理解Java如何工作。 – Raijen