在我的java程序中遇到静态问题

时间:2016-02-16 22:33:33

标签: java static

我写了一个java程序,从一组饭菜中随机选择,决定早餐,午餐和晚餐吃什么。每餐都有营养价值(卡路里,碳水化合物e.t.c)。

在我的节目随机选择当天的膳食计划之后,我希望它打印出它所选择的早餐选项,它选择的午餐选项以及它选择的晚餐选项。然后它将显示当天的营养总量。这是我收到错误的地方Cannot make a static reference to the non-static field fp。我理解为什么我收到此错误但不知道如何修复它。

我的饭菜尚未创建,所以现在我只是模拟它,输入营养素的随机值。

我有2个班,我的膳食班:

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

}

1 个答案:

答案 0 :(得分:1)

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

SELECT t.diff
     , t.company_id
  FROM (
         SELECT IF(r.company_id = @pv_company_id, r.created_at - @pv_created_at, NULL) AS diff
              , IF(r.company_id = @pv_company_id, 1, 0) AS include_
              , @pv_company_id := r.company_id          AS company_id
              , @pv_created_at := r.created_at          AS created_at
              , r.id                                    AS id
           FROM (SELECT @pv_company_id := NULL, @pv_created_at := NULL) i
          CROSS
           JOIN Transactions r
          ORDER
             BY r.company_id
              , r.id
       ) t
 WHERE t.include_
 ORDER BY t.id

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

FoodPlan

这些早餐不是每个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

您要在<{1}}内创建一个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并在那里使用它。