【问题标题】:How to use a series of while loops to ask questions of the user?如何使用一系列的while循环向用户提问?
【发布时间】:2019-03-23 16:40:21
【问题描述】:

我想问用户三个问题

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





 }
}

}

【问题讨论】:

  • 欢迎来到 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


【解决方案1】:

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

    }

【讨论】:

    【解决方案2】:

    为了便于维护,最好使用java集合。

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Map;
    import java.util.Scanner;
    import java.util.Set;
    import java.util.TreeMap;
    
    public class Survey {
    
        public static void main(String[] args) {
    
            List<String> questions = Arrays.asList("Are you happy or sad today?", "Are you short or tall?", "Are you strong or weak");
            List<Set<String> > answers = Arrays.asList(new HashSet<String>(Arrays.asList("happy", "sad")),
                    new HashSet<String>(Arrays.asList("short", "tall")),
                    new HashSet<String>(Arrays.asList("strong", "weak"))
            );
            List<String> replies = new ArrayList<String>();
            Map<String, String> conclusions = new TreeMap<String, String>();
            conclusions.put("[happy, short, weak]", "You are a short happy person who is weak: I suggest more exercise! ");
            conclusions.put("[sad, tall, strong]", "You are a sad strong person who is tall: I suggest hugging a tree!");
            conclusions.put("[sad, short, weak]", "You are a sad short person who is weak: I am sorry to hear that");
    
            Scanner scan = new Scanner(System.in); 
            do {
                int questionNo = replies.size();
                System.out.println(questions.get(questionNo));
                String reply = scan.nextLine().toLowerCase(); //Waits for input
                if(answers.get(questionNo).contains(reply))
                    replies.add(reply);
    
            } while (replies.size() < questions.size());
    
            String conclusion = conclusions.get(replies.toString());
            if (conclusion != null){
                System.out.println(conclusion);
            }
            scan.close();
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2018-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-03
      • 1970-01-01
      • 2022-07-21
      • 1970-01-01
      相关资源
      最近更新 更多