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

时间:2015-02-01 23:12:46

标签: java unique-constraint

我正在使用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";
    }

}

6 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分: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);

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

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

答案 4 :(得分:0)

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

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

答案 5 :(得分:0)

完成了。 我设置了一些修复原始问题的变量。

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

}

相关问题