【问题标题】:How to avoid returning the same string twice in a row如何避免连续两次返回相同的字符串
【发布时间】:2015-02-01 23:12:46
【问题描述】:

我正在使用 Java 制作 SimpleEliza 图表框。我已经完成了老师要求的所有事情,除了我需要对方法 askQuestion 进行编程,使其不连续两次从数组中返回相同的字符串。我不确定如何解决这个问题。你能给我一些建议吗。

下面是 simpleEliza 类的代码。为了避免混淆,simpleEliza 从不同的(Launcher)类中提取。不过这应该没关系。

public class SimpleEliza {
    int questionNumber=0;

    public boolean hasMoreQuestions() {

        while (true) {
            questionNumber ++;
        if (questionNumber == 6) break;
            return true;}
            System.out.println("I'm done leave me alone.");
            return false;   
    }


    public String askQuestion(){
        String[] questionList = {"How are you doing?", "Have you been doing this long?",
                                "Do you like this class?", "What is you name?",
                                "What do you think about computers?"};
        return questionList[(int)(Math.random()*questionList.length)];
    }

    public String listen(String statement){

        // Positive Responses
        if (statement.toLowerCase().contains("like"))
                return ("That's nice!");
        if (statement.toLowerCase().contains ("love"))
                return ("That's nice!");
        if (statement.toLowerCase().contains ("excellent"))
                return ("That's nice!");
        if (statement.toLowerCase().contains ("good"))
                return ("That's nice!");

        //Negative Responses
        if (statement.toLowerCase().contains("dislike"))
                return ("Yikes");
        if (statement.toLowerCase().contains("hate"))
                return ("Yikes");
        if (statement.toLowerCase().contains("do not like"))
                return ("Yikes");

        return "Uh Huh";
    }

}

【问题讨论】:

  • 将您用 rand 选择的索引保存在一个静态变量中,以备下次使用。然后再次调用 random 直到它具有不同的索引。保存新索引。

标签: java unique-constraint


【解决方案1】:

只需在类中保留最后一个返回值的索引,然后检查最后一个是否等于当前值。

int lastIndex = -1;//add this global to your class, not the method.

int currIndex = (int)(Math.random()*questionList.length);
do
{
   result = questionList[currIndex];
}
while (lastIndex == currIndex);

顺便说一句,您应该更喜欢我的方法,而不是将最后一个字符串保留为 Java 缓存整数,从 -128 到 127,这将使您的程序使用的内存比创建用于比较的对象要少;)

【讨论】:

    【解决方案2】:

    存储上一个问题的索引,并不断提取条目,直到你得到一个不同的条目。

    【讨论】:

    • 你能说得更清楚些吗?
    • 有不清楚的地方,还是希望我给你写代码?
    【解决方案3】:

    如何保留最后一个问题的索引值,该值最初为 -1。然后,如果问题是最后一个问题,请选择其他问题。

    //  At class level
    int lastQuestion = -1;
    
    public String askQuestion(){
      int result = -1; 
      String[] questionList = {"How are you doing?", "Have you been doing this   long?",
                                "Do you like this class?", "What is you name?",
                                "What do you think about computers?"};
      do{
        result = (int)(Math.random()*questionList.length);
      }
      while (result != lastQuestion);
    
      lastQuestion = result;
    
      return questionList[result];
    }
    

    【讨论】:

    • 我已经尝试了你的建议。 Eclipse 不会提示任何错误,但它也不会启动程序。
    • 再次尝试并让程序运行,但这不起作用。它仍然一遍又一遍地返回相同的问题。
    • 尝试使用 Random 对象而不是 Math 的 random 方法:docs.oracle.com/javase/8/docs/api/java/util/Random.html
    • result = new Random(System.currentTimeMillis()).nextInt(questionList.length - 1);
    【解决方案4】:

    最简单的方法是使用一个额外的变量。此变量存储您最近选择的索引。现在,在您的方法中传递此变量并检查生成的随机数是否与变量中的值相同。如果是,则重新运行此随机数生成,直到条件变为 false。

    【讨论】:

      【解决方案5】:

      随机字符串的问题是你仍然有可能两次得到相同的东西。

      一个简单的选项是记住最后一个响应,并为每个条件提供两个可能的响应。如果最后的回答是选项 A,请改为选项 B。

      【讨论】:

        【解决方案6】:

        完成了。 我设置了更多变量来解决原始问题。

        public class SimpleEliza {
        int questionNumber=0;
        int questions = 0;
        int previous = 0;
        int asked = 0;
        
        public boolean hasMoreQuestions() {
        
            while (true) {
                questionNumber ++;
            if (questionNumber == 5) break;
                return true;}
                System.out.println("I'm done leave me alone.");
                return false;   
        }
        
        
        public String askQuestion(){
            String[] questionList = {"How are you doing?", "Have you been doing this long?",
                    "Do you like this class?", "What is you name?",
                    "What do you think about computers?", "Do you like college?"};
            int asked = (int) (Math.random() * questionList.length);
            while (previous == asked)
                asked = (int) (Math.random() * questionList.length);
            previous = asked;
        
            return questionList[asked];
        

        }

        public String listen(String statement){
        
            // Positive Responses
            if (statement.toLowerCase().contains("adore"))
                    return ("That's nice!");
            if (statement.toLowerCase().contains ("love"))
                    return ("That's nice!");
            if (statement.toLowerCase().contains ("enjoy"))
                    return ("That's nice!");
        
        
            //Negative Responses
            if (statement.toLowerCase().contains("dislike"))
                    return ("Yikes");
            if (statement.toLowerCase().contains("hate"))
                    return ("Yikes");
            if (statement.toLowerCase().contains("despise"))
                    return ("Yikes");
        
            return "Uh Huh";
        }
        

        }

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-11-17
          • 2022-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多