2016-06-21 45 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