方法没有被正确调用?! 卡路里应用程序

huangapple go评论72阅读模式
英文:

Method not being called properly?! Calorie App

问题

package com.company;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        System.out.println("今天摄入了多少卡路里?");
        int actualIntake = scan.nextInt();
        System.out.println("你的基础代谢率是多少?");
        int BMR = scan.nextInt();
        scan.close();

        calorieCalculation(actualIntake, BMR);
        actualCalories(actualIntake, BMR);
        gym(30, 40, 50, 100, actualIntake);
        testingMeth(actualIntake);
    }

    public static int calorieCalculation(int actualIntake, int BMR) {
        int calorieDifference = BMR - actualIntake;

        if (calorieDifference <= 0) {
            calorieDifference = Math.abs(BMR - actualIntake);
            System.out.println("你的摄入超过了所需量,好样的,超过了" + calorieDifference + "卡路里。");
        } else if (calorieDifference >= 0) {
            System.out.println("预期的卡路里赤字应为" + calorieDifference);
        }

        return calorieDifference;
    }

    public static int actualCalories(int actualIntake, int BMR) {

        int deficitCalculation = actualIntake - BMR;
        if (actualIntake > BMR) {
            System.out.println("你这个胖家伙,别吃太多了,你个蠢货,超过了" + deficitCalculation + "卡路里的赤字。");
        } else if (actualIntake < BMR) {
            System.out.println("干得好,你创造了" + deficitCalculation + "的赤字,继续保持。");
        }
        return deficitCalculation;
    }

    public static int gym(int treadMillCal, int rowingMachineCal, int weightsCal, int proteinShakeCal, int actualIntake) {

        int totalGym = ((treadMillCal + rowingMachineCal + weightsCal) - proteinShakeCal);

        if (totalGym >= 50) {
            System.out.println("干得好,喝蛋白奶昔的同时你燃烧了超过50卡路里。");
        } else if (totalGym < 50) {
            System.out.println("喝蛋白奶昔有什么意义呢,如果你把卡路里补回去了,你瘦不了: 你燃烧了" + totalGym + "卡路里。");
        }

        int gymAndTotal = actualIntake - totalGym;
        System.out.println("你摄入的食物,减去你锻炼消耗的热量以及摄入的蛋白质,剩余" + gymAndTotal);

        return totalGym;
    }

    public static void testingMeth(int actualIntake) {
        System.out.println(actualIntake);
    }
}

这是您提供的Java程序的中文翻译部分。如果您有任何其他问题或需要进一步帮助,请随时问我。

英文:
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
//making values easier to change and also create global variables for gym comparison
Scanner scan = new Scanner (System.in);
System.out.println(&quot;How many calories did you consume today?&gt;&gt; &quot;);
int actualIntake = scan.nextInt();
System.out.println(&quot;What is your BMR?&gt;&gt; &quot;);
int BMR = scan.nextInt();
scan.close();
//this method is what is expected with deficit
calorieCalculation(actualIntake,BMR);
//this is what you actually ate
actualCalories(actualIntake,BMR);
//gym with protein
gym (30,40,50,100, actualIntake);
}
//testing method
testingMeth(actualIntake);
//What the user should be following
public static int calorieCalculation(int actualIntake, int BMR){
int calorieDifference = BMR - actualIntake;
if (calorieDifference &lt;= 0 ){
calorieDifference = Math.abs (BMR - actualIntake);
System.out.println(&quot;You have went over your deficit, well done fatty = &quot; + calorieDifference);
} else if (calorieDifference &gt;= 0){
System.out.println(&quot;Expected calorie deficit should be &quot; + calorieDifference);
}
return calorieDifference;
}
//What the user actually did
public static int actualCalories (int actualIntake, int BMR ) {
int deficitCalculation = actualIntake - BMR;
if (actualIntake &gt; BMR ) {
System.out.println(&quot;You fat lard stop overeating you dumbass, &quot; + &quot;failed deficit of over &quot; + deficitCalculation + &quot; Calories.&quot;);
} else if (actualIntake &lt; BMR ) {
System.out.println(&quot;Well done you created a deficit of &quot; + deficitCalculation + &quot; keep her going keep her movin.&quot; );
}
return deficitCalculation;
}
//How much did you burn in the gym
public static int gym (int treadMillCal, int rowingMachineCal, int weightsCal, int proteinShakeCal, int actualIntake) {
int totalGym = ((treadMillCal + rowingMachineCal + weightsCal) - proteinShakeCal);
if (totalGym &gt;= 50 ) {
System.out.println(&quot;Well done you have burned more than 50 calories whilst drinking protein shake&quot;);
} else if (totalGym &lt; 50 ) {
System.out.println(&quot;Whats the bloody point of drinking protein if your putting the calories back on fatty: &quot; + totalGym + &quot; calories is how much you lost&quot;);
}
int gymAndTotal = actualIntake - totalGym;
System.out.println(&quot;What you ate, plus minusing your workout along with the protein you consumed &quot; + gymAndTotal);
return totalGym;
}
public static void testingMeth (int actualIntake) {
System.out.println(actualIntake);
}
}
//Take calories in then calculate BMR and compare, return value

So I am currently learning java, just learning and making random calorie deficit and BMR program. I created a new method called:

public static int testingMeth(actualIntake) {
System.out.println(actualIntake);
}

The issue is when i try to call the method after the gym method, it creates an error.

    gym (30,40,50,100, actualIntake);
}
testingMeth(actualIntake);

If i was to delete the gym method from the main method, all my other methods has errors. I do not necessarily need a solution for this program but rather why am i receiving these errors? Just want to learn and improve! Thanks.

In other words, I can call the testingMeth before the Gym method and it works fine, but why not after the gym method? and if i get rid of the gym method, multiple errors occur amongst the other methods within the program?

答案1

得分: 0

如果您查看下面的代码,我可以按任何顺序运行两种方法,而且运行得很好。

您需要阅读有关方法声明和方法调用基础的内容。

它会对您有所帮助。

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        //使值更容易更改,还可以为健身比较创建全局变量

        Scanner scan = new Scanner(System.in);
        System.out.println("今天消耗了多少卡路里?>> ");
        int actualIntake = scan.nextInt();
        System.out.println("您的BMR是多少?>> ");
        int BMR = scan.nextInt();
        scan.close();

        //这个方法是预期的带有赤字的方法
        calorieCalculation(actualIntake, BMR);
        //这是您实际吃的
        testingMeth(actualIntake);
        actualCalories(actualIntake, BMR);
        //健身房与蛋白质
        gym(30, 40, 50, 100, actualIntake);
    }

    //测试方法


    //用户应该遵循的内容
    public static int calorieCalculation(int actualIntake, int BMR) {
        int calorieDifference = BMR - actualIntake;

        if (calorieDifference <= 0) {
            calorieDifference = Math.abs(BMR - actualIntake);
            System.out.println("您超过了您的赤字,干得好,胖子 = " + calorieDifference);
        } else if (calorieDifference >= 0) {
            System.out.println("预期的卡路里赤字应为 " + calorieDifference);
        }

        return calorieDifference;
    }

    //用户实际做的
    public static int actualCalories(int actualIntake, int BMR) {

        int deficitCalculation = actualIntake - BMR;
        if (actualIntake > BMR) {
            System.out.println("你这个胖家伙别再吃太多了,笨蛋,超过了赤字 " + deficitCalculation + " 卡路里。");

        } else if (actualIntake < BMR) {
            System.out.println("干得好,你创造了 " + deficitCalculation + " 的赤字,继续保持。");
        }
        return deficitCalculation;
    }

    //您在健身房燃烧了多少卡路里
    public static int gym(int treadMillCal, int rowingMachineCal, int weightsCal, int proteinShakeCal, int actualIntake) {

        int totalGym = ((treadMillCal + rowingMachineCal + weightsCal) - proteinShakeCal);

        if (totalGym >= 50) {
            System.out.println("干得好,您在喝蛋白质摇奶时燃烧了50卡路里以上");
        } else if (totalGym < 50) {
            System.out.println("如果您在摄入卡路里,喝蛋白质有何用途,胖子:" + totalGym + " 卡路里是您失去的量");
        }

        int gymAndTotal = actualIntake - totalGym;
        System.out.println("您吃的食物,减去您的锻炼以及您消耗的蛋白质 " + gymAndTotal);

        return totalGym;
    }

    public static void testingMeth(int actualIntake) {
        System.out.println(actualIntake);
    }

}

您需要明白每个类/方法/switch-case/条件的开放大括号必须有相应的闭合大括号。

在您的情况下,您试图在类的闭合大括号之后调用某些方法,所以这些元素不是您类的一部分,这就是为什么会抛出错误。

英文:

If you see below code, i am able to run both method in any sequence and it's working fine as well.

You need to go through with basics of method declaration and method call.

It will help you.

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
//making values easier to change and also create global variables for gym comparison
Scanner scan = new Scanner(System.in);
System.out.println(&quot;How many calories did you consume today?&gt;&gt; &quot;);
int actualIntake = scan.nextInt();
System.out.println(&quot;What is your BMR?&gt;&gt; &quot;);
int BMR = scan.nextInt();
scan.close();
//this method is what is expected with deficit
calorieCalculation(actualIntake, BMR);
//this is what you actually ate
testingMeth(actualIntake);
actualCalories(actualIntake, BMR);
//gym with protein
gym(30, 40, 50, 100, actualIntake);
}
//testing method
//What the user should be following
public static int calorieCalculation(int actualIntake, int BMR) {
int calorieDifference = BMR - actualIntake;
if (calorieDifference &lt;= 0) {
calorieDifference = Math.abs(BMR - actualIntake);
System.out.println(&quot;You have went over your deficit, well done fatty = &quot; + calorieDifference);
} else if (calorieDifference &gt;= 0) {
System.out.println(&quot;Expected calorie deficit should be &quot; + calorieDifference);
}
return calorieDifference;
}
//What the user actually did
public static int actualCalories(int actualIntake, int BMR) {
int deficitCalculation = actualIntake - BMR;
if (actualIntake &gt; BMR) {
System.out.println(&quot;You fat lard stop overeating you dumbass, &quot; + &quot;failed deficit of over &quot; + deficitCalculation + &quot; Calories.&quot;);
} else if (actualIntake &lt; BMR) {
System.out.println(&quot;Well done you created a deficit of &quot; + deficitCalculation + &quot; keep her going keep her movin.&quot;);
}
return deficitCalculation;
}
//How much did you burn in the gym
public static int gym(int treadMillCal, int rowingMachineCal, int weightsCal, int proteinShakeCal, int actualIntake) {
int totalGym = ((treadMillCal + rowingMachineCal + weightsCal) - proteinShakeCal);
if (totalGym &gt;= 50) {
System.out.println(&quot;Well done you have burned more than 50 calories whilst drinking protein shake&quot;);
} else if (totalGym &lt; 50) {
System.out.println(&quot;Whats the bloody point of drinking protein if your putting the calories back on fatty: &quot; + totalGym + &quot; calories is how much you lost&quot;);
}
int gymAndTotal = actualIntake - totalGym;
System.out.println(&quot;What you ate, plus minusing your workout along with the protein you consumed &quot; + gymAndTotal);
return totalGym;
}
public static void testingMeth(int actualIntake) {
System.out.println(actualIntake);
}
}

You need to understand for every opening braces of class/method/switch-case/or condition must have closing braces.

In your case you are try to call some method after closing braces of class, so those elements are not part of your class and that's why it's throwing an error.

huangapple
  • 本文由 发表于 2020年8月20日 01:31:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63492147.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定