java - 要求用户输入一个字符串以表明他有更多的数据要输入,或者他是否完成了
问题描述
一切正常,但我需要帮助制作一个循环,使用户能够输入所有食物和食物的值。另外,有些代码无法在这里发布,所以我截取了它。 https://prnt.sc/r6nptj
System.out.println("Are you male or female? (M/F)");
gender = kboard.next();
System.out.println("Enter your weight in lbs.");
weight = kboard.nextDouble();
System.out.println("Enter your height in inches.");
height = kboard.nextDouble();
System.out.println("Enter your age in years.");
age = kboard.nextDouble();
BMRw = 655+(4.35 * weight)+(4.7 * height)-(4.7 * age);
BMRm = 665+(6.23 * weight)+(12.7 * height)-(6.8 * age);
boolean isMale = gender.startsWith("M");
if (isMale == true) {
BMRm = 665+(6.23 * weight)+(12.7 * height)-(6.8 * age);
System.out.println(" Your Basal Metabolism Rate is " + BMRm);
double noExercise = (BMRm * 1.2);
System.out.println("Your Basal Metabolism Rate when you don't engange in exercise is " + noExercise);
double lightExercise = (BMRm * 1.375);
System.out.println("Your Basal Metabolism Rate when you engange in light exercises one to three days a week " + lightExercise);
double intensely = (BMRm * 1.725);
System.out.println(" Your Basal Metabolism Rate when you exercise intensely six to seven days a week " + intensely);
double activeJob = (BMRm * 1.9);
System.out.println("Your Basal Metabolism Rate when you exercise intensely six to seven days a week while having a physically active job " + activeJob);
}
else {
BMRw = 655+(4.35 * weight)+(4.7 * height)-(4.7 * age);
System.out.println(" Your Basal Metabolism Rate is " + BMRw);
double noExercise = (BMRw * 1.2);
System.out.println("Your Basal Metabolism Rate when you don't engange in exercise is " + noExercise);
double lightExercise = (BMRw * 1.375);
System.out.println("Your Basal Metabolism Rate when you engange in light exercises one to three days a week " + lightExercise);
double moderateExercise = (BMRw * 1.55);
System.out.println(" Your Basal Metabolism Rate when you exercise moderately three to five times a week " + moderateExercise);
double intensely = (BMRw * 1.725);
System.out.println(" Your Basal Metabolism Rate when you exercise intensely six to seven days a week " + intensely);
double activeJob = (BMRw * 1.9);
System.out.println("Your Basal Metabolism Rate when you exercise intensely six to seven days a week while having a physically active job " + activeJob);
}
} }
解决方案
好的,所以我也是新手,但我学到的是你可以在你的代码周围放置一个 do...while 循环。
do
{
//put all your code you want in the loop here
//Then, add this:
System.out.print("Would you like to enter more data? (Y/N)");
char answer1 = Expo.enterChar(); //The expo class is a separate class that you will
//need to download/import. You can also look at the
//Expo.html file that I will attach the link for at
//the end to see if you can do it in a different way.
}
while(answer1 == 'N')
{
System.out.println("\nThank you for your input.");
break;
}
现在,这可能行得通,但如果不行,请告诉我。好的,我找不到链接,但这里是Expo.enterChar()
您可能需要的信息:
enterChar() 允许在文本窗口中从键盘输入字符。示例: System.out.print("你的中间名首字母是什么?--> "); char middleInitial = Expo.enterChar(); 检索在键盘上输入的第一个字符并将其存储在 char 变量 middleInitial 中。
推荐阅读
- vapor - 如何用 Vapor 过滤孩子的其他父母?
- mongodb - mongorestore 操作是否添加到 oplog?
- angular - 悬停时角度材质按钮更改
- string - 找到 K 的最大值,使得子序列 A 和 B 存在并应满足上述条件
- python - 从列表中删除引号 - Python
- google-chrome - Chrome SameSite Cookie
- simulation - 如何从 ANYLOGIC 中的代理列表中获取代理索引
- python - 数学公式中的 mod 代表什么以及使用它 python
- java - Mvn clean package 给出了一个意外的 java.lang.AssertionError
- azure-devops - 代理在 catalina MacOS 中给出一个非零值(-126)作为状态