java - 如何使用一系列的while循环来向用户提问?
问题描述
我想问用户的三个问题
- 你今天是开心还是难过?
- 你是矮还是高
- 你是强还是弱
作为调查的输出,我想将他们的结果显示为带有标点符号的句子以及对用户的单一推荐(如果可以选择,请使用复合词)。
这是我到目前为止所拥有的:
public static void main(String[] args) {
String reply;
String reply2;
String reply3;
Scanner scan = new Scanner(System.in);
System.out.println("Are you happy or sad today?");
reply = scan.nextLine(); //Waits for input
System.out.println("Are you short or tall");
reply2 = scan.nextLine();
System.out.println("Are you strong or weak");
reply3 = scan.nextLine();
if (reply.equalsIgnoreCase("Happy") && reply2.equalsIgnoreCase("Weak")&& reply3.equalsIgnoreCase("Short") ){
System.out.println("You are a short happy person who is weak: I suggest more exercise ");
}else if (reply.equalsIgnoreCase("sad") && reply2.equalsIgnoreCase("strong")&& reply3.equalsIgnoreCase("tall") ){
System.out.println("You are a sad strong person who is tall: I suggest hugging a tree");
} else {
System.out.println("Incorrect!" );
}
}
}
我需要帮助弄清楚如何使用 awhile-loop
来提问,并且我的代码需要清理。
发布反馈:
package javaapplication13;
public static void main(String[] args) {
String reply;
String reply2;
String reply3;
Scanner scan = new Scanner(System.in);
do {
System.out.println("Are you happy or sad today?");
reply = scan.nextLine(); //Waits for input
} while (!(reply.equalsIgnoreCase("happy") || reply.equalsIgnoreCase("sad")));
do {
System.out.println("Are you short or tall?");
reply2 = scan.nextLine();
} while (!(reply2.equalsIgnoreCase("short") || reply2.equalsIgnoreCase("tall")));
do{
System.out.println("Are you strong or weak");
reply3 = scan.nextLine();
} while (!(reply3.equalsIgnoreCase("strong") || reply3.equalsIgnoreCase("weak")));
if (reply.equalsIgnoreCase("Happy") && reply2.equalsIgnoreCase("short")&& reply3.equalsIgnoreCase("weak") ){
System.out.println("You are a short happy person who is weak: I suggest more exercise! ");
} else if (reply.equalsIgnoreCase("sad") && reply2.equalsIgnoreCase("tall")&& reply3.equalsIgnoreCase("strong") ){
System.out.println("You are a sad strong person who is tall: I suggest hugging a tree!");
} else if (reply.equalsIgnoreCase("sad") && reply2.equalsIgnoreCase("short")&& reply3.equalsIgnoreCase("weak") ){
System.out.println("You are a sad short person who is weak: I am sorry to hear that");
}
}
}
解决方案
由于您已经知道如何使用 while 循环,因此在代码中添加 3 个布尔变量来表示情绪、身高和力量将稍微清理代码。
boolean emotion;
boolean height;
boolean strength;
在 do-while 循环中,如果回复匹配“快乐”,则将布尔值设置为 true,否则,将其设置为 false,这显然是“悲伤”。其他2个问题相同。
do {
System.out.println("Are you happy or sad today?");
reply = scan.nextLine(); // Waits for input
if (reply.equalsIgnoreCase("Happy")) {
emotion = true;
} else {
emotion = false;
}
} while (!(reply.equalsIgnoreCase("happy") || reply.equalsIgnoreCase("sad")));
do {
System.out.println("Are you short or tall?");
reply2 = scan.nextLine();
if (reply.equalsIgnoreCase("Tall")) {
height = true;
} else {
height = false;
}
} while (!(reply2.equalsIgnoreCase("short") || reply2.equalsIgnoreCase("tall")));
do {
System.out.println("Are you strong or weak");
reply3 = scan.nextLine();
if (reply.equalsIgnoreCase("Strong")) {
strength = true;
} else {
strength = false;
}
} while (!(reply3.equalsIgnoreCase("strong") || reply3.equalsIgnoreCase("weak")))
然后,当谈到最后一部分时,您必须考虑 3 个答案的组合,而不必编写像这样的长代码
if(reply.equalsIgnoreCase("Happy") && reply2.equalsIgnoreCase("short")&& reply3.equalsIgnoreCase("weak"))
您可以简单地检查布尔值来确定您的建议。
if (emotion && !height && !strength) {
System.out.println("You are a short happy person who is weak: I suggest more exercise! ");
} else if (!emotion&&strength&&height) {
System.out.println("You are a sad strong person who is tall: I suggest hugging a tree!");
} else if (!emotion&&!height&&!strength) {
System.out.println("You are a sad short person who is weak: I am sorry to hear that");
}
推荐阅读
- finite-automata - 具有非常复杂语言的确定性有限自动机
- javascript - 在 Blazor 服务器端运行 JavaScript 库 (Looper) - 一些 JavaScript 代码未运行
- python - 如何使用 Python 在 Tableau 中更改我的工作表筛选器?
- python - 正则表达式可选组不匹配
- html - 如何获取电子邮件域功能
- python - 等值线图的颜色没有改变
- javascript - 调试此代码。y1有什么问题?
- wordpress - 我想在我的网站上使用类似的地图,有人可以指导我如何将它与传单地图一起使用
- powerbi - 使用同一表中的 2 个不同选择作为 DAX (PBI) 中的过滤器计算度量
- perl - if 命令未按需要执行。也许是由于非局部变量?