我有一个代码,要求用户一个接一个地输入多个值,这对 N 个用户重复,直到 (-1) 输入循环内的任何值。下面是我的代码,没有循环,因为我不知道该怎么做,它也没有计数器来找出 N 会是什么(我稍后会添加它)。
public class Test{
public static void main(String[] args){
int id;
int tempId;
int age;
int tempAge;
int carbs;
int fat;
int protein;
int totalGrams;
int totalCalories;
int sumGrams = 0;
int sumCalories = 0;
double percentCarbs;
double percentFat;
double percentProtein;
double ratio = 0;
double tempRatio;
Scanner in = new Scanner (System.in);
System.out.print("Please enter your ID:");
tempId = in.nextInt();
System.out.print("Please enter your age:");
tempAge = in.nextInt();
System.out.print("Please enter the amount of carbohydrates consumed (in grams):");
carbs = in.nextInt();
System.out.print("Please enter the amount of fat consumed (in grams):");
fat = in.nextInt();
System.out.print("Please enter the amount of protein consumed (in grams):");
protein = in.nextInt();
totalGrams = TotalGrams(carbs, fat, protein);
totalCalories = TotalCalories(carbs, fat, protein);
sumGrams += totalGrams;
sumCalories += totalCalories;
percentCarbs = Percentage(carbs*4, totalCalories);
percentFat = Percentage(fat*9, totalCalories);
percentProtein = Percentage(protein*4, totalCalories);
tempRatio = ProteinEnergyRatio(carbs, fat, protein);
System.out.println("\nThe total number of Grams consumed is "+totalGrams +" and the total number of Calories consumed is "+totalCalories);
System.out.println("The percentage of each nutrient form the total amount of calories is as follows:");
System.out.println("Carbohydrates make "+percentCarbs + "\t\tFats make "+percentFat + "\t\tProteins make "+percentProtein);
if(tempRatio >= ratio){
ratio = tempRatio;
id = tempId;
age = tempAge;
}
}
public static int TotalGrams(int x, int y, int z){
return (x + y + z);
}
public static int TotalCalories(int x, int y, int z){
return (x*4) + (y*9) + (z*4);
}
public static double Percentage(int x, int y){
return (double)x / y * 100;
}
public static double ProteinEnergyRatio(int x, int y, int z){
return z / (x + y);
}
}
我不确定我是否正确理解了您,但据我了解,您希望在每次用户输入输入时进行评估,以做出退出应用程序或继续旅程的决定。如果是这种情况,您可以扩展您要求用户输入的功能并在该功能内做出决定。像这样:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
public class test{
public static void main(String[] args){
int id;
int tempId;
int age;
int tempAge;
int carbs;
int fat;
int protein;
int totalGrams;
int totalCalories;
int sumGrams = 0;
int sumCalories = 0;
double percentCarbs;
double percentFat;
double percentProtein;
double ratio = 0;
double tempRatio;
Scanner in = new Scanner (System.in);
while(true){
System.out.print("Please enter your ID:");
tempId = nextInt(in);
System.out.print("Please enter your age:");
tempAge = nextInt(in);
System.out.print("Please enter the amount of carbohydrates consumed (in grams):");
carbs = nextInt(in);
System.out.print("Please enter the amount of fat consumed (in grams):");
fat = nextInt(in);
System.out.print("Please enter the amount of protein consumed (in grams):");
protein = nextInt(in);
totalGrams = TotalGrams(carbs, fat, protein);
totalCalories = TotalCalories(carbs, fat, protein);
sumGrams += totalGrams;
sumCalories += totalCalories;
percentCarbs = Percentage(carbs*4, totalCalories);
percentFat = Percentage(fat*9, totalCalories);
percentProtein = Percentage(protein*4, totalCalories);
tempRatio = ProteinEnergyRatio(carbs, fat, protein);
System.out.println("\nThe total number of Grams consumed is "+totalGrams +" and the total number of Calories consumed is "+totalCalories);
System.out.println("The percentage of each nutrient form the total amount of calories is as follows:");
System.out.println("Carbohydrates make "+percentCarbs + "\t\tFats make "+percentFat + "\t\tProteins make "+percentProtein);
if(tempRatio >= ratio){
ratio = tempRatio;
id = tempId;
age = tempAge;
}
}
}
public static int nextInt(Scanner in){
int val = in.nextInt();
if(val == -1)
System.exit(0);
return val;
}
public static int TotalGrams(int x, int y, int z){
return (x + y + z);
}
public static int TotalCalories(int x, int y, int z){
return (x*4) + (y*9) + (z*4);
}
public static double Percentage(int x, int y){
return (double)x / y * 100;
}
public static double ProteinEnergyRatio(int x, int y, int z){
return z / (x + y);
}
}
因此,因为您问如何才能跳出循环,所以我将为这种情况添加另一个解决方案。有多种方法可以实现这一点,但我认为在任何编程语言中使用异常都非常强大,并且可以使您的代码看起来更加干净和易于理解。
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
public class test{
public static void main(String[] args){
int id;
int tempId;
int age;
int tempAge;
int carbs;
int fat;
int protein;
int totalGrams;
int totalCalories;
int sumGrams = 0;
int sumCalories = 0;
double percentCarbs;
double percentFat;
double percentProtein;
double ratio = 0;
double tempRatio;
Scanner in = new Scanner (System.in);
while(true){
try {
System.out.print("Please enter your ID:");
tempId = nextInt(in);
System.out.print("Please enter your age:");
tempAge = nextInt(in);
System.out.print("Please enter the amount of carbohydrates consumed (in grams):");
carbs = nextInt(in);
System.out.print("Please enter the amount of fat consumed (in grams):");
fat = nextInt(in);
System.out.print("Please enter the amount of protein consumed (in grams):");
protein = nextInt(in);
totalGrams = TotalGrams(carbs, fat, protein);
totalCalories = TotalCalories(carbs, fat, protein);
sumGrams += totalGrams;
sumCalories += totalCalories;
percentCarbs = Percentage(carbs*4, totalCalories);
percentFat = Percentage(fat*9, totalCalories);
percentProtein = Percentage(protein*4, totalCalories);
tempRatio = ProteinEnergyRatio(carbs, fat, protein);
System.out.println("\nThe total number of Grams consumed is "+totalGrams +" and the total number of Calories consumed is "+totalCalories);
System.out.println("The percentage of each nutrient form the total amount of calories is as follows:");
System.out.println("Carbohydrates make "+percentCarbs + "\t\tFats make "+percentFat + "\t\tProteins make "+percentProtein);
if(tempRatio >= ratio){
ratio = tempRatio;
id = tempId;
age = tempAge;
}
} catch (Exception e) {
System.err.println(e.getMessage());
break;
}
}
}
public static int nextInt(Scanner in) throws Exception{
int val = in.nextInt();
if(val == -1)
throw new Exception("Invalid Input");
return val;
}
public static int TotalGrams(int x, int y, int z){
return (x + y + z);
}
public static int TotalCalories(int x, int y, int z){
return (x*4) + (y*9) + (z*4);
}
public static double Percentage(int x, int y){
return (double)x / y * 100;
}
public static double ProteinEnergyRatio(int x, int y, int z){
return z / (x + y);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句