[英]Need help calculating percentages for value
我在編寫被指示作為作業執行的程序時遇到了一些麻煩。
我只是好奇我如何計算用戶輸入的每個值的百分比。 我在試圖找到正確的表達方式時遇到了一些麻煩,以便它可讀並且可以被 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;
正如拉曼所說,既然總和整齊地存儲在一個變量中,那么計算百分比就相對容易了
double carbPercent = ((double)carb / sum) * 100
double fatPercent = ((double)fat / sum) * 100
double proteinPercent = ((double)pro / sum) * 100
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.