简体   繁体   English

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

[英]How to avoid returning the same string twice in a row

I am working on a SimpleEliza chartbox in Java. 我正在用Java开发一个SimpleEliza图表框。 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. 我已经完成了老师所需的一切,除了我需要对方法AskQuestion进行编程以使它不会连续两次从数组中返回相同的字符串。 I am uncertain as how to approach this problem. 我不确定如何解决这个问题。 Can you give me some advice. 你能给我一些建议吗?

Below is the code for the simpleEliza class. 下面是simpleEliza类的代码。 Just to avoid confusion, simpleEliza pulls from a different (Launcher) class. 为了避免混淆,simpleEliza从另一个(Launcher)类中提取。 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";
    }

}

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

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 ;) 顺便说一句,您应该首选我的方法,而不是将最后一个String保留为Java缓存整数(从-128到127),这将使您的程序使用的内存少于创建用于比较的对象;)

How about keeping an index value for the last question asked which would initially be -1. 如何为最后一个询问的索引保留索引值,该索引初始为-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];
}

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. 如果是,请重新运行此随机数生成,直到条件变为假。

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. 如果最后一个响应是选项A,请改用选项B。

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

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM