如何避免连续两次返回相同的字符串 [英] How to avoid returning the same string twice in a row
问题描述
我正在使用 Java 开发 SimpleEliza 图表框.我已经完成了老师要求的所有内容,只是我需要对方法 askQuestion 进行编程,以免连续两次从数组中返回相同的字符串.我不确定如何解决这个问题.你能给我一些建议吗.
下面是 simpleEliza 类的代码.为了避免混淆,simpleEliza 从不同的(启动器)类中拉取.不过那应该没关系.
public class SimpleEliza {int questionNumber=0;公共布尔 hasMoreQuestions() {而(真){问题编号++;如果(问题编号 == 6)中断;返回真;}System.out.println("我完了,别管我了.");返回假;}公共字符串 askQuestion(){String[] questionList = {"你好吗?", "你做了这么久吗?","你喜欢这门课吗?", "你叫什么名字?",你对计算机有什么看法?"};返回 questionList[(int)(Math.random()*questionList.length)];}公共字符串听(字符串语句){//正面回应if (statement.toLowerCase().contains("like"))return ("那太好了!");if (statement.toLowerCase().contains ("love"))return ("那太好了!");if (statement.toLowerCase().contains ("excellent"))return ("那太好了!");if (statement.toLowerCase().contains ("good"))return ("那太好了!");//否定响应if (statement.toLowerCase().contains("dislike"))返回(哎呀");if (statement.toLowerCase().contains("hate"))返回(哎呀");if (statement.toLowerCase().contains("不喜欢"))返回(呀");返回嗯嗯";}}
只需保留类中最后一个返回值的索引,然后检查最后一个是否等于当前值.
int lastIndex = -1;//将此全局添加到您的类中,而不是方法中.int currIndex = (int)(Math.random()*questionList.length);做{结果 = questionList[currIndex];}while (lastIndex == currIndex);
顺便说一句,您应该更喜欢我的方法而不是将最后一个字符串作为 Java
缓存整数从 -128 到 127 这将使您的程序使用的内存少于创建用于比较的对象;)>
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";
}
}
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 ;)
这篇关于如何避免连续两次返回相同的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!