gpt4 book ai didi

java - 需要帮助计算值(value)百分比

转载 作者:行者123 更新时间:2023-11-30 10:57:32 27 4
gpt4 key购买 nike

我在编写作业时遇到了一些问题。

我很好奇如何计算用户输入的每个值的百分比。我在尝试找到正确的措辞方式以使其可读并且可以被 Java 正确解释时遇到了一些麻烦。

我的目标是计算脂肪、碳水化合物和蛋白质的克数各自的百分比,并将其显示在屏幕上。我不是要别人帮我做作业,我只是需要一点指导。

  //local constants

//local variables
int carb; // amount of carbs entered by user
int fat; // amount of fat entered by user
int pro; // amount of protein entered by user
int sum; // sum of three numbers

//myLib = new Library();

/******************** Start main method *****************/

//Prompt user for amount of carbohydrates
System.out.print("Please enter the amount of carbohydrates: ");
carb = Keyboard.readInt();

//Prompt user for amount of fat
System.out.print("Please enter the amount of fat: ");
fat = Keyboard.readInt();

//Prompt user for amount of protein
System.out.print("Please enter amount of protein: ");
pro = Keyboard.readInt();

//Calculate the sum of the three numbers
sum = carb + fat + pro;

最佳答案

正如 Raman 所说,现在计算百分比相对容易,因为您已将总和整齐地存储在一个变量中

double carbPercent = ((double)carb / sum) * 100
double fatPercent = ((double)fat / sum) * 100
double proteinPercent = ((double)pro / sum) * 100

关于java - 需要帮助计算值(value)百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32576570/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com