-3

对不起,如果这个问题是重复的,但我还没有找到答案。我想使用java查找问题的类型。问题是我不知道如何从用户将提交的问题中找到问题的类型。

例如:

Where is the Louvre Museum?

The type of this question is location. 

有人知道怎么做吗?

4

2 回答 2

1

使用用户输入:

Scanner s = new Scanner(System.in)

询问输入问题:

System.out.println("Question:");
String[] q = s.nextLine().split(" "); //changing user input to word array
String w = q[0].toLowerCase();

检查疑问词:

switch(w){
    case "where":
        //print out "this is a location question"
        break;
    case "who":
        //print out "this is a who question"
        break;

    ... //keep on doing this for all words

    default:
        //print out "this is not a valid question"
        break;

}

如果有多个:

System.out.println("Question:");
String[] q = s.nextLine().split(" "); 
List<String> a = Arrays.asList(q);

检查它是否包含疑问词:

if(a.contains("Who"))
    //print out "Who question"
if(a.contains("Where"))
    //print out "Where question"
于 2015-01-17T20:13:17.227 回答
0

您可以考虑使用字典列表,其中将包含成对的问题类型以及与问题类型相对应的特定子字符串,例如:

"yes/no", "is"
"location",  {"where", "how far", "direction"}
"time", {"when", "how long"}

你明白了。对于这种情况,可以使用映射(例如 HashMap)非常容易地完成:

HashMap <String, String[]> hm = new HashMap <String, String[]>();

你可以更换

String[]

ArrayList <String>

也是。

于 2015-01-17T20:15:39.657 回答