当前位置: 首页 > 知识库问答 >
问题:

在我的java程序中遇到静态问题[重复]

艾正浩
2023-03-14

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

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

}

和我的食物计划课:

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个答案

鲁景山
2023-03-14

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

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

这些都不应该是静态的,因为并不是所有的foodplans都有特定的卡路里数,而是每个特定的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());
    ...
}

这里应该使用哪个foodplanmain?您可能应该在main方法中创建FoodPlan并在其中使用它。

 类似资料:
  • 这是我收到的错误消息: 7:42:07 AM:执行任务'Demo.main()'... 执行任务:[Demo.main()]在项目C:\用户\shsh\AndroidStudioProjects\Learnjava2 开始Gradle Daemon... Gradle Daemon在8 s 119 ms中开始 失败:构建失败,但有例外。 > 错误:配置项目“app”时出现问题。 无法创建任务:应用

  • 我试图创建一个基本的计算程序,使用用户输入(几乎是我自己的)和处理非常小的计算。不过,我似乎无法让计算返回。我在这个文件中完成所有的计算: 这个主程序在这里运行: 导入java。util。扫描仪; 即使有用户输入,我也会打印出0.0。我一直在玩弄我的变量和方法,但似乎无法让它发挥作用。任何帮助都将不胜感激!

  • 以下代码的输出是:请输入您的单词: 你好, 凯沙夫, 鲍勃, doan kehsavbob 线程中的异常 “主要” java.lang.数字格式异常: 对于输入字符串: “kehsavbob” 在 java.base/java.lang.数字格式异常.为输入字符串 (数字格式例外.java:67) 在 java.base/java.lang.Integer.parseInt(整数.java:668

  • 我从上周的Flitter开始,我在audioplayer软件包上遇到了这个问题。我正在Udemy和Dart一起参加一个Flight开发训练营。我几乎跟踪了视频中的所有内容,但我收到了一条无法解决的巨大错误信息。 这是我的代码: 这是我收到的错误消息:正在运行Gradle任务“AssembledBug”。。。e:在依赖项中发现不兼容的类。从类路径中删除它们,或者使用“-Xskip metadata

  • 问题内容: 我正在尝试玩 文件如此[处(1)])和here(2)所述。 (1)一个不好的例子: 第一种方法可行,但我不想依赖某些东西。第二个结果是仅播放了一些前导帧,听起来更像是单击。从ByteArrayInputStream播放时,不会是IO问题。 请与您分享为什么会发生这种情况。TIA。 问题答案: 我不确定为什么您链接的第二种方法会启动另一个线程;我相信音频还是会以自己的方式播放。剪辑播放完

  • 你好,我试图选择并点击按钮“立即预订”,但当我查看源代码时,它显示了以下... 当我检查Firefox中的“Book Now”链接时,显示了以下内容 [为什么有两个按钮class=“btn btn-secondary text-大写btn-landing go-to-session”的实例??]