如何在Java中显示脂肪百分比的卡路里。

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

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(&quot;The percent of calories from fat is &quot; + fatPercent + &quot;.&quot;);

}

//getFat function
static int getFat(int fat){

    Scanner keyboard = new Scanner(System.in);

    do{
    System.out.println(&quot;What is the amount of fat grams?&quot;);
    fat = keyboard.nextInt();
    }while (fat &lt; 0);
    
    return fat;
}
//getCalories function
static int getCalories(int calorie){

    Scanner keyboard = new Scanner(System.in);

    do{
    System.out.println(&quot;What is the amount of calories?&quot;);
    calorie = keyboard.nextInt();
    }while ((calorie &gt;= fat * 9) &amp;&amp; (calorie &lt;= 0));
    
    return calorie;
}

}

答案1

得分: 0

问题1:在getCalories中使用的calorie变量具有方法范围,所以基本上你需要将其返回的值分配给你的静态变量。
在主方法中:

fat = getFat();
calorie = getCalories();

同时不需要传递参数,因为从键盘输入获取了值,这似乎有点毫无意义。

问题2:如果未分配任何值,声明为静态calorie的全局值将为0,而你没有为它分配任何值。默认的整数值是0。

不必担心是新手,继续编码 如何在Java中显示脂肪百分比的卡路里。

英文:

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 如何在Java中显示脂肪百分比的卡路里。

huangapple
  • 本文由 发表于 2020年10月22日 23:17:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/64485347.html
匿名

发表评论

匿名网友

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

确定