Java 改进计算器循环的建议?

Java 改进计算器循环的建议?,java,loops,Java,Loops,如果满足某些条件,我只想运行两个内部do while循环。如果运行程序,则始终显示错误消息。如何更正循环,使其仅在不满足条件时运行 整个程序的do while根据字符串答案重复循环。如果程序当前正在运行,循环将重复,但不允许输入dogName。只适合配重。我如何纠正这个问题 public static void main(String[] args) { System.out.println(" Welcome to Chiecoman's Dog Age Calculator&

如果满足某些条件,我只想运行两个内部do while循环。如果运行程序,则始终显示错误消息。如何更正循环,使其仅在不满足条件时运行

  • 整个程序的do while根据字符串答案重复循环。如果程序当前正在运行,循环将重复,但不允许输入dogName。只适合配重。我如何纠正这个问题

     public static void main(String[] args) {
     System.out.println(" Welcome to Chiecoman's Dog Age Calculator");
     System.out.println();
    
    
    //variables for user input
    String dogName;
    int dogWeight=0; 
    double dogAge, humanAge =0;
    Scanner keyboard = new Scanner (System.in);
    String answer;
    
    do{
    // User input begins
    System.out.println("Please enter your dog's name:");
    dogName = keyboard.nextLine();
    
    System.out.println("Please enter your dog's age (1-16).");
    dogAge = keyboard.nextDouble();
    // do-while loop to repeat until condition is met
    do
     {    
     System.out.println("Error: Age is out of Range");
     System.out.println("Please enter the actual dog age (1-16)");
     dogAge= keyboard.nextDouble();    
     }        
     while (dogAge <1 || dogAge >16);
    
    System.out.println("Please enter your dog's weight in lbs");
    dogWeight = keyboard.nextInt();
    // do-while loop to repeat until condition is met
    do
    {
     System.out.println("Error: Weight must be greater than zero");
     System.out.println("Please enter your dog's weight in lbs");
    }    
    while (dogWeight <1);
    // how dog age is calculated
    if (dogAge ==1)
    humanAge=15;
    else if (dogAge >=2 && dogAge <=5)
      humanAge = 4 * dogAge +15;
    else if (dogAge >= 6 && dogAge <= 16 && dogWeight <20 )
      humanAge = 4 * dogAge + 16;
    else if (dogAge >= 6 && dogAge <= 16 && dogWeight >= 21 &&
         dogWeight <= 50)
      humanAge = 4.5 * dogAge + 15;
    else if (dogAge >= 6 && dogAge <=16 && dogWeight > 50)
      humanAge = 7.5 * dogAge;
     // displays dog age
    System.out.println(dogName + "'s age in human years is " +
      humanAge);
    // ask user to calculate more dog ages
    System.out.println("Would you like to calculate the age of another" +
      "dog (Y/N)?");
    answer = keyboard.next();
    }
    while (answer.equalsIgnoreCase("y"));
    
    publicstaticvoidmain(字符串[]args){
    System.out.println(“欢迎使用Chiecoman的狗龄计算器”);
    System.out.println();
    //用于用户输入的变量
    字符串dogName;
    int体重=0;
    双狗龄,人龄=0;
    扫描仪键盘=新扫描仪(System.in);
    字符串回答;
    做{
    //用户输入开始
    System.out.println(“请输入您的狗的名字:”);
    dogName=keyboard.nextLine();
    System.out.println(“请输入您的狗的年龄(1-16)”;
    dogAge=keyboard.nextDouble();
    //执行while循环以重复,直到满足条件
    做
    {    
    System.out.println(“错误:年龄超出范围”);
    System.out.println(“请输入实际犬龄(1-16)”;
    dogAge=keyboard.nextDouble();
    }        
    而(16岁);
    System.out.println(“请以磅为单位输入您的狗的体重”);
    dogWeight=keyboard.nextInt();
    //执行while循环以重复,直到满足条件
    做
    {
    System.out.println(“错误:重量必须大于零”);
    System.out.println(“请以磅为单位输入您的狗的体重”);
    }    
    
    while(dogWeight=2&&dogAge=6&&dogAge请记住,在do-while中,第一次迭代总是发生的。虽然对于代码来说不是最优的,但您可以在下面看到如何解决异常情况

    import java.util.Scanner;
    
    public class DogAgeCalc {
    
        public static void main(String[] args) {
            System.out.println(" Welcome to Chiecoman's Dog Age Calculator");
            System.out.println();
    
    
    //variables for user input
            String dogName;
            int dogWeight = 0;
            double dogAge, humanAge = 0;
            Scanner keyboard = new Scanner(System.in);
            String answer;
    
            do {
    // User input begins
                System.out.println("Please enter your dog's name:");
                dogName = keyboard.nextLine();
    // do-while loop to repeat until condition is met
                boolean firstAgeInput = true;
                do {
                    if (!firstAgeInput) System.out.println("Error: Age is out of Range");
                    System.out.println("Please enter the actual dog age (1-16)");
                    dogAge = keyboard.nextInt();
                    firstAgeInput = false;
                }
                while (dogAge < 1 || dogAge > 16);
    
    
    // do-while loop to repeat until condition is met
                boolean firstWtInput = true;
                do {
                    if (!firstWtInput) System.out.println("Error: Weight must be greater than zero");
                    System.out.println("Please enter your dog's weight in lbs");
                    dogWeight = keyboard.nextInt();
                    firstWtInput = false;
                }
                while (dogWeight < 1);
    
                // how dog age is calculated
                if (dogAge == 1)
                    humanAge = 15;
                else if (dogAge >= 2 && dogAge <= 5)
                    humanAge = 4 * dogAge + 15;
                else if (dogAge >= 6 && dogAge <= 16 && dogWeight < 20)
                    humanAge = 4 * dogAge + 16;
                else if (dogAge >= 6 && dogAge <= 16 && dogWeight >= 21 &&
                        dogWeight <= 50)
                    humanAge = 4.5 * dogAge + 15;
                else if (dogAge >= 6 && dogAge <= 16 && dogWeight > 50)
                    humanAge = 7.5 * dogAge;
                // displays dog age
                System.out.println(dogName + "'s age in human years is " + humanAge);
    // ask user to calculate more dog ages
                System.out.println("Would you like to calculate the age of another dog (Y/N)?");
                answer = keyboard.next();
            }
            while (answer.equalsIgnoreCase("y"));
        }
    }
    
    import java.util.Scanner;
    公共类DogAgeCalc{
    公共静态void main(字符串[]args){
    System.out.println(“欢迎使用Chiecoman的狗龄计算器”);
    System.out.println();
    //用于用户输入的变量
    字符串dogName;
    int体重=0;
    双狗龄,人龄=0;
    扫描仪键盘=新扫描仪(System.in);
    字符串回答;
    做{
    //用户输入开始
    System.out.println(“请输入您的狗的名字:”);
    dogName=keyboard.nextLine();
    //执行while循环以重复,直到满足条件
    布尔值firstAgeInput=true;
    做{
    如果(!firstAgeInput)System.out.println(“错误:年龄超出范围”);
    System.out.println(“请输入实际犬龄(1-16)”;
    dogAge=keyboard.nextInt();
    firstAgeInput=false;
    }
    而(狗龄<1 | |狗龄>16);
    //执行while循环以重复,直到满足条件
    布尔输入=真;
    做{
    if(!firstWtInput)System.out.println(“错误:权重必须大于零”);
    System.out.println(“请以磅为单位输入您的狗的体重”);
    dogWeight=keyboard.nextInt();
    firstWtInput=false;
    }
    而(体重<1);
    //如何计算狗的年龄
    如果(dogAge==1)
    人龄=15岁;
    否则,如果(dogAge>=2&&dogAge=6&&dogAge=6&&dogAge=21&&
    小狗体重=6(50只)
    人龄=7.5*狗龄;
    //显示狗的年龄
    System.out.println(dogName+“,在人类年龄中的年龄是“+人类年龄);
    //请用户计算更多狗的年龄
    System.out.println(“您想计算另一只狗的年龄(Y/N)?”;
    答案=键盘。下一步();
    }
    while(答案:等信号情况(“y”));
    }
    }
    
    (1)如果不满足条件,可以使用while循环 (2) 你需要跳过空行

    public static void main(String[] args) {
        System.out.println(" Welcome to Chiecoman's Dog Age Calculator");
        System.out.println();
        
        
        //variables for user input
        String dogName;
        int dogWeight=0; 
        double dogAge, humanAge =0;
        Scanner keyboard = new Scanner (System.in);
        String answer;
        
        do{
            // User input begins
            System.out.println("Please enter your dog's name:");
            dogName = keyboard.nextLine();
        
            System.out.println("Please enter your dog's age (1-16).");
            dogAge = keyboard.nextDouble();
            // do-while loop to repeat until condition is met
            while(dogAge <1 || dogAge >16){
                System.out.println("Error: Age is out of Range");
                System.out.println("Please enter the actual dog age (1-16)");
                dogAge= keyboard.nextDouble(); 
            }
        
            System.out.println("Please enter your dog's weight in lbs");
            dogWeight = keyboard.nextInt();
            // do-while loop to repeat until condition is met
            while(dogWeight <1){
                System.out.println("Error: Weight must be greater than zero");
                System.out.println("Please enter your dog's weight in lbs");
                dogWeight= keyboard.nextInt(); 
            }
            // how dog age is calculated
            if (dogAge ==1)
            humanAge=15;
            else if (dogAge >=2 && dogAge <=5)
                humanAge = 4 * dogAge +15;
            else if (dogAge >= 6 && dogAge <= 16 && dogWeight <20 )
                humanAge = 4 * dogAge + 16;
            else if (dogAge >= 6 && dogAge <= 16 && dogWeight >= 21 &&
                    dogWeight <= 50)
                humanAge = 4.5 * dogAge + 15;
            else if (dogAge >= 6 && dogAge <=16 && dogWeight > 50)
                humanAge = 7.5 * dogAge;
            // displays dog age
            System.out.println(dogName + "'s age in human years is " + humanAge);
            // ask user to calculate more dog ages
            System.out.println("\nWould you like to calculate the age of another dog (Y/N)?");
            answer = keyboard.next();
            keyboard.skip("(\r\n)");  //to skip blank lines
        }
        while (answer.equalsIgnoreCase("y"));
    }
    
    publicstaticvoidmain(字符串[]args){
    System.out.println(“欢迎使用Chiecoman的狗龄计算器”);
    System.out.println();
    //用于用户输入的变量
    字符串dogName;
    int体重=0;
    双狗龄,人龄=0;
    扫描仪键盘=新扫描仪(System.in);
    字符串回答;
    做{
    //用户输入开始
    System.out.println(“请输入您的狗的名字:”);
    dogName=keyboard.nextLine();
    System.out.println(“请输入您的狗的年龄(1-16)”;
    dogAge=keyboard.nextDouble();
    //执行while循环以重复,直到满足条件
    while(16岁){
    System.out.println(“错误:年龄超出范围”);
    System.out.println(“请输入实际犬龄(1-16)”;
    dogAge=keyboard.nextDouble();
    }
    System.out.println(“请以磅为单位输入您的狗的体重”);
    dogWeight=keyboard.nextInt();
    //执行while循环以重复,直到满足条件
    
    while(dogWeight=2&&dogAge=6&&dogAge确保您的重量输入在do-while循环的内部,如下所示

        bool initialWeightInput=true; // to avoid run into error message on first iteration
        // do-while loop to repeat until condition is met
        do
        {
          if(!initialWeightInput)
            System.out.println("Error: Weight must be greater than zero");
    
          initialWeightInput=false;
          System.out.println("Please enter your dog's weight in lbs");
          dogWeight = keyboard.nextInt();
        }    
        while (dogWeight <1);
    
    bool initialWeightInput=true;//避免在第一次迭代时遇到错误消息
    //执行while循环以重复,直到满足条件
    做
    {
    如果(!initialWeightInput)
    System.out.println(“错误:重量必须大于零”);
    initialWeightInput=false;
    System.out.println(“请以磅为单位输入您的狗的体重”);
    dogWeight=keyboard.nextInt();
    }    
    (配重)