java - 改进计算器循环的建议?
问题描述
如果满足某些条件,我只想运行两个内部 do-while 循环。如果运行程序,则始终显示错误消息。如果不满足条件,您将如何更正循环以仅运行?
整个程序的 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"));
解决方案
请记住,在 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"));
}
}
推荐阅读
- java - 获取异常预期单个匹配 bean 但发现 2
- mongodb - 我在 MongoDB 中有一系列文档,例如
- javascript - 根据输入字段一一显示 3 div
- python - 未找到:/addTodoItem/
- flutter - 即使在对 4 个元素使用 setState 后,设置状态在底部导航栏项目中也不起作用
- python - Plotly:表面颜色如何使最小颜色值透明?
- python - 在 docker 环境中找不到匹配的功能集,python selenium
- amazon-web-services - AWC EC2 Amazon Linux 2 实例在应用操作系统更新后无法启动
- arraylist - F#洗牌功能
- java - 具有自定义用户联合 docker 部署的 Keycloak