【发布时间】:2019-03-23 16:40:21
【问题描述】:
我想问用户三个问题
- 你今天开心还是难过?
- 你是矮还是高
- 你是强还是弱
作为调查的输出,我想将他们的结果显示为带有标点符号的句子以及对用户的单一建议(如果可以选择,请使用复合词)。
这是我目前所拥有的:
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!" );
}
}
}
我需要帮助弄清楚如何使用while-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");
}
}
}
【问题讨论】:
-
欢迎来到 SO!要在 while 循环中执行此操作,您需要一个从 1 开始并在每次迭代中递增的“计数器”变量(通常是 i)。对于循环的每次迭代,如果 i=1 则问第一个问题,如果 i=2 则问第二个问题,如果 i=3 则问第三个问题。然后根据提出的问题(reply、reply2、reply3)将每个响应存储在自己的变量中。然后在循环之后,使用收集的响应编译你的句子。如果您需要更多帮助,请尝试使用 while 循环并发布您的新代码
-
您的代码考虑的答案有更多组合:[happy, strong, tall], [happy, strong, short], [happy, weak, tall], [happy,弱,矮],[悲伤,强壮,高大],..等。目前尚不清楚您对所有组合的建议是什么。顺便说一句,很容易一一提出问题并将结果打印在一个语句中......
-
另外,是什么决定了最后的“我建议”评论?请参阅上面@zlakad 的评论,这正是我要问的。
标签: java if-statement while-loop