英文:
How do I display calories from fat percent in Java
问题
新手上路,有2个问题。
问题1:
在程序要求输入卡路里数量的地方,代码中 "while" 部分的运算符应该在两边都为真时继续询问卡路里数量。但实际情况并非如此。我该如何修复?例如,如果我输入脂肪为20,卡路里为100,它应该一遍又一遍地询问,直到我输入180或更多(20 * 9)。
问题2:
在 getCalories 函数完成后,我收到以下错误消息:
"Exception in thread "main" java.lang.ArithmeticException: / by zero
at fatGramCalculator.main(fatGramCalculator.java:19)"
我知道不能除以零,但我认为当我输入脂肪为20时,它会在整个程序中保持脂肪的值?
抱歉问题有点长,但我觉得我离解决方法很近了。感谢您提供任何反馈。
import java.util.Scanner;
public class fatGramCalculator{
static int fat, calorie, fatPercent;
//Module main
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
//调用 getFat 函数
fat = getFat(fat);
//调用 getCalories 函数
calorie = getCalories(calorie);
//计算脂肪百分比
fatPercent = ((fat * 9) / calorie);
System.out.println("脂肪占总卡路里的百分比为:" + fatPercent + "。");
}
//getFat 函数
static int getFat(int fat){
Scanner keyboard = new Scanner(System.in);
do{
System.out.println("请输入脂肪克数:");
fat = keyboard.nextInt();
}while (fat < 0);
return fat;
}
//getCalories 函数
static int getCalories(int calorie){
Scanner keyboard = new Scanner(System.in);
do{
System.out.println("请输入卡路里数量:");
calorie = keyboard.nextInt();
}while ((calorie >= fat * 9) && (calorie <= 0));
return calorie;
}
}
英文:
Newbie here with 2 questions.
Question 1:
Where the program asks for the amount of calories, the operator in the "while" portion of the code is supposed to keep asking for the amount of calories until both sides of the operator is true. It is not. How do I fix that? example... If I enter fat at 20, and calories at 100, It should ask me again and again until I enter 180 or more (20 * 9).
Question 2:
I receive this error message after the getCalories function is completed:
"Exception in thread "main" java.lang.ArithmeticException: / by zero
at fatGramCalculator.main(fatGramCalculator.java:19)"
I understand you can not divide by zero but I thought when I entered fat at 20 it remained as the value of fat throughout the program?
Sorry kind of lengthy but I feel like I am very close to the working. Thank you for any and all feedback.
import java.util.Scanner;
public class fatGramCalculator{
static int fat, calorie, fatPercent;
//Module main
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
//Calls getFat function
getFat(fat);
//Calls getCalories function
getCalories(calorie);
//shows percent of fat
fatPercent = ((fat * 9) / calorie);
System.out.println("The percent of calories from fat is " + fatPercent + ".");
}
//getFat function
static int getFat(int fat){
Scanner keyboard = new Scanner(System.in);
do{
System.out.println("What is the amount of fat grams?");
fat = keyboard.nextInt();
}while (fat < 0);
return fat;
}
//getCalories function
static int getCalories(int calorie){
Scanner keyboard = new Scanner(System.in);
do{
System.out.println("What is the amount of calories?");
calorie = keyboard.nextInt();
}while ((calorie >= fat * 9) && (calorie <= 0));
return calorie;
}
}
答案1
得分: 0
问题1:在getCalories
中使用的calorie
变量具有方法范围,所以基本上你需要将其返回的值分配给你的静态变量。
在主方法中:
fat = getFat();
calorie = getCalories();
同时不需要传递参数,因为从键盘输入获取了值,这似乎有点毫无意义。
问题2:如果未分配任何值,声明为静态calorie
的全局值将为0,而你没有为它分配任何值。默认的整数值是0。
不必担心是新手,继续编码
英文:
Question 1: the calorie
variable you are using in the getCalories has a method scope, so basically you need to assign the returned value from it to your static variable
In the main method :
fat = getFat();
calorie = getCalories();
And also don't pass it arguments, it seems kinda pointless because you are getting the value from keyboard input.
Question 2: global value declared as static calorie if not assigned anything will be 0, and you are not assigning it any value. Default int value is 0.
Don't worry for being newbie, keep coding
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论