如何使用一系列的while循环来询问用户问题?

时间:2018-10-18 19:39:42

标签: java if-statement while-loop

我想问用户三个问题

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

我需要帮助弄清楚如何使用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");





 }
}

}

2 个答案:

答案 0 :(得分:0)

由于您已经弄清楚了如何使用while循环, 在代码中添加3个布尔值变量,分别代表情绪,身高和力量,这将使代码有所整理。

    boolean emotion;
    boolean height;
    boolean strength;

在do-while循环中,如果reply匹配“ Happy”,则将布尔值设置为true,否则将其设置为false,这显然会是“ sad”。其他两个问题也一样。

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

    }

答案 1 :(得分:0)

为便于维护,请更好地利用Java集合。

2*inputSize - 1