当用户在循环内的任何输入中输入-1时如何停止Java程序?

samasthtc

我有一个代码,要求用户一个接一个地输入多个值,这对 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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

当用户在scanf()中输入错误的数据类型时,如何解决无限循环?

当用户输入字符而不是数字时 C 中的无限循环

当用户输入退出或退出 c# 时跳出循环

当用户在C中输入Ctrl + d时退出程序

当用户输入“退出”,当问名字如何结束循环?

当用户输入“退出”一词时,如何使用 Java 中的扫描器退出 do-while 循环?

当用户在登录时输入错误的用户名和密码时,它未在ROR中闪烁任何错误消息

当用户在editText中输入任意金额时,如何附加$?

当用户按下提交按钮时,如何在 $alert 内的表单中显示用户提交的 $name 输入?

当用户停止输入 swift 时如何创建函数

当用户在C ++中按下“输入”时,如何读取带空格的序列并停止?

当用户只输入空格时验证 rails 中的表单

用户输入的数字乘积,当用户输入0时程序停止

当用户输入输入时,如何在for循环外打印输出

如何在Java中循环用户输入

tkinter -当用户开始在输入字段内输入时,如何使按钮状态从禁用状态变为“正常”?

如何在Java程序中基于用户输入继续循环

当用户输入无效输入时如何终止程序?

当用户以Java输入和打印时,循环跳过索引0

当用户输入某个字符串时,如何使while循环退出?

当用户按下Python中的Tab键时,如何接收结束的用户输入?

当用户在python中单击“确定”时,如何保存用户文本框输入?

当用户尝试在 iPhone 上的输入中输入文本时,输入字段旁边的按钮被切断

如何使用递归循环代码(当用户输入无效输入时,它将再次提示他们)?

当用户输入209,312,414时,程序需要停止

当用户单击输入时如何聚焦输入?

在Websphere中,当用户输入无效的URL时,如何更改重定向到的页面?

当用户在jQuery中输入受限字符时,如何显示div?

当用户单击 react.js 中的按钮时如何动态添加输入字段