英文:
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("How many calories did you consume today?>> ");
int actualIntake = scan.nextInt();
System.out.println("What is your BMR?>> ");
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 <= 0 ){
calorieDifference = Math.abs (BMR - actualIntake);
System.out.println("You have went over your deficit, well done fatty = " + calorieDifference);
} else if (calorieDifference >= 0){
System.out.println("Expected calorie deficit should be " + calorieDifference);
}
return calorieDifference;
}
//What the user actually did
public static int actualCalories (int actualIntake, int BMR ) {
int deficitCalculation = actualIntake - BMR;
if (actualIntake > BMR ) {
System.out.println("You fat lard stop overeating you dumbass, " + "failed deficit of over " + deficitCalculation + " Calories.");
} else if (actualIntake < BMR ) {
System.out.println("Well done you created a deficit of " + deficitCalculation + " keep her going keep her movin." );
}
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 >= 50 ) {
System.out.println("Well done you have burned more than 50 calories whilst drinking protein shake");
} else if (totalGym < 50 ) {
System.out.println("Whats the bloody point of drinking protein if your putting the calories back on fatty: " + totalGym + " calories is how much you lost");
}
int gymAndTotal = actualIntake - totalGym;
System.out.println("What you ate, plus minusing your workout along with the protein you consumed " + 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("How many calories did you consume today?>> ");
int actualIntake = scan.nextInt();
System.out.println("What is your BMR?>> ");
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 <= 0) {
calorieDifference = Math.abs(BMR - actualIntake);
System.out.println("You have went over your deficit, well done fatty = " + calorieDifference);
} else if (calorieDifference >= 0) {
System.out.println("Expected calorie deficit should be " + calorieDifference);
}
return calorieDifference;
}
//What the user actually did
public static int actualCalories(int actualIntake, int BMR) {
int deficitCalculation = actualIntake - BMR;
if (actualIntake > BMR) {
System.out.println("You fat lard stop overeating you dumbass, " + "failed deficit of over " + deficitCalculation + " Calories.");
} else if (actualIntake < BMR) {
System.out.println("Well done you created a deficit of " + deficitCalculation + " keep her going keep her movin.");
}
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 >= 50) {
System.out.println("Well done you have burned more than 50 calories whilst drinking protein shake");
} else if (totalGym < 50) {
System.out.println("Whats the bloody point of drinking protein if your putting the calories back on fatty: " + totalGym + " calories is how much you lost");
}
int gymAndTotal = actualIntake - totalGym;
System.out.println("What you ate, plus minusing your workout along with the protein you consumed " + 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论