How to avoid returning the same string twice in a row

青春壹個敷衍的年華 提交于 2019-12-12 04:25:21

问题


I am working on a SimpleEliza chartbox in Java. I have finished everything that my teacher has required except that I need to program the method askQuestion to not return the same string from the array twice in a row. I am uncertain as how to approach this problem. Can you give me some advice.

Below is the code for the simpleEliza class. Just to avoid confusion, simpleEliza pulls from a different (Launcher) class. That shouldn't matter though.

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:


Simply keep an index of the last returned value in your class then check if the last is equals to current.

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

By the way you should prefer my method over keeping the last String as Java cache integers from -128 to 127 which will make your program use less memory than creating an object for comparison ;)




回答2:


Store the index of the previous question, and keep pulling entries until you get one that's different.




回答3:


How about keeping an index value for the last question asked which would initially be -1. Then if the question is the last question, pick a different one.

//  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];
}



回答4:


simplest way would be to make use of an extra variable. This variable stores your recently selected index. Now, pass this variable in your method and check if random number generated is same as the value in variable. If yes, re-run this random number generation until condition becomes false.




回答5:


The problem with random strings is there is still a chance you will get the same thing twice.

A simple option is to remember the last response and have two possible responses for each condition. If the last response was option A, give option B instead.




回答6:


Finished it. I set some more variables which fixed the original problem.

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

}



来源:https://stackoverflow.com/questions/28269071/how-to-avoid-returning-the-same-string-twice-in-a-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!