如何避免连续两次返回相同的字符串

How to avoid returning the same string twice in a row

我正在 Java 中开发 SimpleEliza 图表框。我已经完成了老师要求的所有内容,除了我需要将方法 askQuestion 编程为不连续两次 return 来自数组的相同字符串。我不确定如何解决这个问题。你能给我一些建议吗?

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

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

}

如何保留最后一个问题的索引值,最初为 -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];
}

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

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

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 的缓存整数,这将使您的程序使用比创建用于比较的对象更少的内存;)

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

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

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

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

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

}