I have a code where the user is asked to enter multiple values one after the other, this repeats for N number of users until (-1) is entered into any of the values inside the loop. Below is my code, without a loop because I'm not sure how to go about this, it's also without a counter to find out what N would be (i'll add it later).
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);
}
}
CodePudding user response:
I'm not sure if I understood you correctly, but from what I understood is that you want to evaluate each time the user enters an input to make a decision wether to exit the application or continue the journey. If that's the case, you can sort of extend the function where you ask the user for the input and make a decision inside that function. Like this:
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);
}
}
So because you asked how can you just break out of the loop, I'm going to add another solution for this scenario. There are various ways to achieve this but I think using exceptions is really powerful in any programming language and makes your code looks a lot more clean and understandable.
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);
}
}