我的java程序中存在静态错误

我的java程序中存在静态错误,java,static,Java,Static,我已经编写了一个java程序,它从一组食物中随机选择,以决定早餐、午餐和晚餐吃什么。每顿饭都有营养价值(卡路里、碳水化合物等) 在我的程序随机选择了当天的膳食计划后,我希望它打印出它选择了什么样的早餐选项、它选择了什么样的午餐选项以及它选择了什么样的晚餐选项。然后它将显示当天的营养总量。这里是我得到错误的地方,无法对非静态字段fp进行静态引用。我理解为什么会出现这个错误,但不知道如何修复它 我的膳食还没有创建,所以现在我只是模拟它,输入营养素的随机值 我有两门课,我的进餐课: public cl

我已经编写了一个java程序,它从一组食物中随机选择,以决定早餐、午餐和晚餐吃什么。每顿饭都有营养价值(卡路里、碳水化合物等)

在我的程序随机选择了当天的膳食计划后,我希望它打印出它选择了什么样的早餐选项、它选择了什么样的午餐选项以及它选择了什么样的晚餐选项。然后它将显示当天的营养总量。这里是我得到错误的地方,
无法对非静态字段fp进行静态引用。我理解为什么会出现这个错误,但不知道如何修复它

我的膳食还没有创建,所以现在我只是模拟它,输入营养素的随机值

我有两门课,我的进餐课:

public class Meal {

    int calories, carbs, sugar, protein, fat;

    public Meal (int calories, int carbs, int sugar, int protein, int fat) {
        this.calories = calories;
        this.carbs = carbs;
        this.sugar = sugar;
        this.protein = protein;
    }

    public int getCalories() {
        return calories;
    }

    public int getCarbs() {
        return calories;
    }

    public int getSugar() {
        return calories;
    }

    public int getProtein() {
        return calories;
    }

    public int getFat() {
        return calories;
    }

}
还有我的FoodPlan课程:

import java.util.Random;

public class FoodPlan {

    public FoodPlan(Meal breakfast, Meal lunch, Meal dinner) {
        this.breakfast = breakfast;
        this.lunch = lunch;
        this.dinner = dinner;
    }

    int b = 3;
    int l = 3;
    int d = 3;

    Meal breakfast;
    Meal lunch;
    Meal dinner;

    static int calories;
    static int carbs;
    static int sugar;
    static int protein;
    static int fat;

    Random rand = new Random();

    Meal breakfast1 = new Meal(rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200));
    Meal breakfast2 = new Meal(rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200));
    Meal breakfast3 = new Meal(rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200));

    Meal lunch1 = new Meal(rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200));
    Meal lunch2 = new Meal(rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200));
    Meal lunch3 = new Meal(rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200));

    Meal dinner1 = new Meal(rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200));
    Meal dinner2 = new Meal(rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200));
    Meal dinner3 = new Meal(rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200));

    FoodPlan fp = new FoodPlan(breakfast, lunch, dinner);


    public void setBreakfast(Meal Meal) {
        this.breakfast = Meal;
    }

    public void setLunch(Meal Meal) {
        this.lunch = Meal;
    }

    public void setDinner(Meal Meal) {
        this.dinner = Meal;
    }


    public Meal getBreakfast() {
        return breakfast;
    }

    public Meal getLunch() {
        return lunch;
    }

    public Meal getDinner() {
        return dinner;
    }


    public void breakfast() {

        int r = rand.nextInt(b) + 1;

        if (r == 1) {
            fp.setBreakfast(breakfast1);
        }

        if (r == 2) {
            fp.setBreakfast(breakfast2);
        }

        if (r == 3) {
            fp.setBreakfast(breakfast3);
        }
    }


    public void lunch() {

        int r = rand.nextInt(b) + 1;

        if (r == 1) {
            fp.setLunch(lunch1);
        }

        if (r == 2) {
            fp.setLunch(lunch2);
        }

        if (r == 3) {
            fp.setLunch(lunch3);
        }
    }


    public void dinner() {

        int r = rand.nextInt(b) + 1;

        if (r == 1) {
            fp.setDinner(dinner1);
        }

        if (r == 2) {
            fp.setDinner(dinner2);
        }

        if (r == 3) {
            fp.setDinner(dinner3);
        }
    }


    public int getCalorieTotal() {
        calories = breakfast.getCalories() + lunch.getCalories() + dinner.getCalories();
        return calories;
    }

    public int getCarbsTotal() {
        carbs = breakfast.getCarbs() + lunch.getCarbs() + dinner.getCarbs();
        return carbs;
    }

    public int getSugarTotal() {
        sugar = breakfast.getSugar() + lunch.getSugar() + dinner.getSugar();
        return sugar;
    }

    public int getProteinTotal() {
        protein = breakfast.getProtein() + lunch.getProtein() + dinner.getProtein();
        return protein;
    }

    public int getFatTotal() {
        fat = breakfast.getFat() + lunch.getFat() + dinner.getFat();
        return fat;
    }


    public static void main(String[] args) {    
        System.out.println("Menu Today:");
        System.out.print("Breakfast: " + fp.getBreakfast());
        System.out.print("Lunch: " + fp.getLunch());
        System.out.print("Dinner: " + fp.getDinner());
        System.out.println("Total Calories: " + fp.getCalorieTotal());
        System.out.println("Total Carbs: " + fp.getCarbsTotal());
        System.out.println("Total Sugar: " + fp.getSugarTotal());
        System.out.println("Total Protein: " + fp.getProteinTotal());
        System.out.println("Total Fat: " + fp.getFatTotal());
    }

}
更新的FoodPlan类:

import java.util.Random;

public class FoodPlan {

    public FoodPlan(Meal breakfast, Meal lunch, Meal dinner) {
        this.breakfast = breakfast;
        this.lunch = lunch;
        this.dinner = dinner;
    }

    int b = 3;
    int l = 3;
    int d = 3;

    Meal breakfast;
    Meal lunch;
    Meal dinner;

    int calories;
    int carbs;
    int sugar;
    int protein;
    int fat;

    Random rand = new Random();

    Meal breakfast1 = new Meal(rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200));
    Meal breakfast2 = new Meal(rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200));
    Meal breakfast3 = new Meal(rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200));

    Meal lunch1 = new Meal(rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200));
    Meal lunch2 = new Meal(rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200));
    Meal lunch3 = new Meal(rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200));

    Meal dinner1 = new Meal(rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200));
    Meal dinner2 = new Meal(rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200));
    Meal dinner3 = new Meal(rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200));

    FoodPlan fp = new FoodPlan(breakfast, lunch, dinner);


    public void setBreakfast(Meal Meal) {
        this.breakfast = Meal;
    }

    public void setLunch(Meal Meal) {
        this.lunch = Meal;
    }

    public void setDinner(Meal Meal) {
        this.dinner = Meal;
    }


    public Meal getBreakfast() {
        return breakfast;
    }

    public Meal getLunch() {
        return lunch;
    }

    public Meal getDinner() {
        return dinner;
    }


    public void breakfast() {

        int r = rand.nextInt(b) + 1;

        if (r == 1) {
            fp.setBreakfast(breakfast1);
        }

        if (r == 2) {
            fp.setBreakfast(breakfast2);
        }

        if (r == 3) {
            fp.setBreakfast(breakfast3);
        }
    }


    public void lunch() {

        int r = rand.nextInt(b) + 1;

        if (r == 1) {
            fp.setLunch(lunch1);
        }

        if (r == 2) {
            fp.setLunch(lunch2);
        }

        if (r == 3) {
            fp.setLunch(lunch3);
        }
    }


    public void dinner() {

        int r = rand.nextInt(b) + 1;

        if (r == 1) {
            fp.setDinner(dinner1);
        }

        if (r == 2) {
            fp.setDinner(dinner2);
        }

        if (r == 3) {
            fp.setDinner(dinner3);
        }
    }


    public int getCalorieTotal() {
        calories = breakfast.getCalories() + lunch.getCalories() + dinner.getCalories();
        return calories;
    }

    public int getCarbsTotal() {
        carbs = breakfast.getCarbs() + lunch.getCarbs() + dinner.getCarbs();
        return carbs;
    }

    public int getSugarTotal() {
        sugar = breakfast.getSugar() + lunch.getSugar() + dinner.getSugar();
        return sugar;
    }

    public int getProteinTotal() {
        protein = breakfast.getProtein() + lunch.getProtein() + dinner.getProtein();
        return protein;
    }

    public int getFatTotal() {
        fat = breakfast.getFat() + lunch.getFat() + dinner.getFat();
        return fat;
    }


    public static void main(String[] args) {

        FoodPlan fp = new FoodPlan(breakfast, lunch , dinner);

        fp.breakfast();
        fp.lunch();
        fp.dinner();

        System.out.println("Menu Today:");
        System.out.print("Breakfast: " + fp.getBreakfast());
        System.out.print("Lunch: " + fp.getLunch());
        System.out.print("Dinner: " + fp.getDinner());
        System.out.println("Total Calories: " + fp.getCalorieTotal());
        System.out.println("Total Carbs: " + fp.getCarbsTotal());
        System.out.println("Total Sugar: " + fp.getSugarTotal());
        System.out.println("Total Protein: " + fp.getProteinTotal());
        System.out.println("Total Fat: " + fp.getFatTotal());
    }

}

变量应该是静态的,当且仅当它对于类是通用的,而不是与该类的特定实例关联时

static int calories;
static int carbs;
static int sugar;
static int protein;
static int fat;
所有这些都不应该是静态的,因为并不是所有的
FoodPlan
s都有特定数量的卡路里,而是每个特定的
FoodPlan
都有特定数量的卡路里

Meal breakfast1 = new Meal(rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200));
Meal breakfast2 = new Meal(rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200));
Meal breakfast3 = new Meal(rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200));
这些早餐不是每个
FoodPlan
的一部分,它们是与特定
FoodPlan
相关联的特定早餐。看起来这些实际上应该是
main
方法中的局部变量

FoodPlan fp = new FoodPlan(breakfast, lunch, dinner);
这里您正在
FoodPlan
内部创建一个
FoodPlan
。这是完全没有必要的

public static void main(String[] args) {    
    System.out.println("Menu Today:");
    System.out.print("Breakfast: " + fp.getBreakfast());
    ...
}

这里应该使用的
FoodPlan
main
?您可能应该在您的
main
方法中创建一个
FoodPlan
,并在那里使用它。

我已将所有变量设置为非静态变量,并将main更改为:创建一个新的FoodPlan实例,然后在此实例上调用我的早餐、午餐和晚餐方法,但现在它要求我将我的膳食变量设置为早餐,午餐和晚餐static@JohnSmith,用修改后的代码更新您的问题?我已经发布了更新的代码class@JohnSmith您希望新的
main
方法中的
breaken
从何而来?您正在呼叫新的FoodPlan(早餐、午餐、晚餐)
;你希望在这里通过什么考试?哪种早餐?@JohnSmith,没有。您可以创建早餐并将其传递给构造函数,也可以停止将早餐传递给构造函数——将构造函数更改为
MealPlan()
,然后在设置早餐后将早餐从
MealPlan
中取出。