当用户在循环内的任何输入中输入 -1 时如何停止 Java 程序?
How to stop Java program when the user enters -1 into any inputs inside a loop?
我有一个代码,要求用户一个接一个地输入多个值,这对 N 个用户重复,直到 (-1) 输入 any 循环内的值。
下面是我的代码,没有循环,因为我不确定如何去做,它也没有计数器来找出 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);
}
}
我有一个代码,要求用户一个接一个地输入多个值,这对 N 个用户重复,直到 (-1) 输入 any 循环内的值。 下面是我的代码,没有循环,因为我不确定如何去做,它也没有计数器来找出 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);
}
}