首页 > 解决方案 > 如何使用一系列的while循环来向用户提问?

问题描述

我想问用户的三个问题

  1. 你今天是开心还是难过?
  2. 你是矮还是高
  3. 你是强还是弱

作为调查的输出,我想将他们的结果显示为带有标点符号的句子以及对用户的单一推荐(如果可以选择,请使用复合词)。

这是我到目前为止所拥有的:

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





 }
}

}

标签: javaif-statementwhile-loop

解决方案


由于您已经知道如何使用 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");

    }

推荐阅读