For loop 在HashMapJava中如何在终端中输入多个键以输出多个值
程序概述:终端询问用户“出了什么问题”,用户给出一个单词“bug”,这是一个键!从hashmapif它是一个键,然后它从终端屏幕中的hashmap输出相应的“值”,否则它会给出一个随机的defaultresponse 问题:如果用户输入'bug and crash',它将只读取'bug'键并输出相应的值,但我想让它做的是拾取多个关键字并输出相应的值,我该怎么做 下面是这两个类所需的代码 来自测试类的方法For loop 在HashMapJava中如何在终端中输入多个键以输出多个值,for-loop,foreach,hashmap,key,return-value,For Loop,Foreach,Hashmap,Key,Return Value,程序概述:终端询问用户“出了什么问题”,用户给出一个单词“bug”,这是一个键!从hashmapif它是一个键,然后它从终端屏幕中的hashmap输出相应的“值”,否则它会给出一个随机的defaultresponse 问题:如果用户输入'bug and crash',它将只读取'bug'键并输出相应的值,但我想让它做的是拾取多个关键字并输出相应的值,我该怎么做 下面是这两个类所需的代码 来自测试类的方法 public HashMap<String, String> create
public HashMap<String, String> createresponseMap(){
HashMap<String,String> responseMap = new HashMap <String,String>();
responseMap.put("crash",
"Well, it never crashes on our system. It must have something\n" +
"to do with your system. Tell me more about your configuration.");
responseMap.put("crashes",
"Well, it never crashes on our system. It must have something\n" +
"to do with your system. Tell me more about your configuration.");
responseMap.put("slow",
"I think this has to do with your hardware. Upgrading your system");
responseMap.put("performance",
"Performance was quite adequate in all our tests. Are you running\n"+
"any other processes in the background?");
responseMap.put("bug",
"Well, you know, all software has some bugs.\n" +
"are working very hard to fix them. Can you describe the problem\n" +
"further?");
responseMap.put("windows",
"This is a known bug to do with the Windows operating system.\n"+
"report it to Microsoft. There is nothing we can do about this.");
responseMap.put("mac",
"This is a known bug to do with the Mac operating system. Please\n" +
"report it to Apple. There is nothing we can do about this.");
responseMap.put("expensive",
"The cost of our product is quite competitive. Have you\n" +
"really compared our features?");
responseMap.put("Netbeans",
"Ahhh, BlueJ, yes. We tried to buy out those guys long ago, but\n" +
"they simply won't sell... Stubborn people they are. Nothing \n" +
"do about it, I'm afraid.");
return responseMap;
}
还有来自另一个类的代码
import java.util.HashMap;
import java.util.HashSet;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
public class Responder
{
// Used to map key words to responses.
private HashMap<String, String> responseMap;
// Default responses to use if we don't recognise a word.
private ArrayList<String> defaultResponses;
private Random randomGenerator;
private ArrayList<String> uninterestingwords;
/**
* Construct a Responder
*/
public Responder(Response response)
{
responseMap = response.createresponseMap();
defaultResponses = response.CreateDefaultResponse();
uninterestingwords = response.CreateInterestingWords();
randomGenerator = new Random();
}
/**
* Generate a response from a given set of input words.
*
* @param words A set of words entered by the user
* @return A string that should be displayed as the response
*/
public String generateResponse(HashSet<String> words)
{
for (String word : words) {
String responses = responseMap.get(word);
if(responses != null && uninterestingwords.indexOf(word)>-1) {
return responses;
}
}
// If we get here, none of the words from the input line was recognized.
// In this case we pick one of our default responses (what we say when
// we cannot think of anything else to say...)
return pickDefaultResponse();
}
private String pickDefaultResponse()
{
// Pick a random number for the index in the default response list.
// The number will be between 0 (inclusive) and the size of the list (exclusive).
int index = randomGenerator.nextInt(defaultResponses.size());
return defaultResponses.get(index);
}
}
我假设publicstringgeneratereresponsehashset单词的方法需要改变,但是什么和如何改变呢
谢谢