首页 > 解决方案 > 改进计算器循环的建议?

问题描述

  1. 如果满足某些条件,我只想运行两个内部 do-while 循环。如果运行程序,则始终显示错误消息。如果不满足条件,您将如何更正循环以仅运行?

  2. 整个程序的 do-while 会根据字符串 Answer 重复循环。如果程序当前正在运行,则循环重复,但不允许输入 dogName。仅适用于 dogWeight。我该如何纠正?

     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"));
    

标签: javaloops

解决方案


请记住,在 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"));
    }
}

推荐阅读