【发布时间】: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 直到它具有不同的索引。保存新索引。