我想要置换键盘输入的字样,并与列表的元素进行比较。我如何使用groovy来做到这一点?
答案 0 :(得分:0)
是的,猜测你的意思,这可以让你输入一行文字。文本行被分成单词,然后打印出来;
以下是代码:
def words = [ 'tim', 'yates' ]
def enteredWords = System.console()?.readLine( 'Enter some words: ' ).tokenize()
def intersection = words.intersect( enteredWords )
def nonintersection = enteredWords - intersection
def missing = words - enteredWords
println "Words you entered that are in my list: $intersection"
println "Words you entered that are not in my list: $nonintersection"
println "Words you missed from my list: $missing"
因此,对于输入This is typed by tim
,输出为:
Words you entered that are in my list: [tim]
Words you entered that are not in my list: [This, is, typed, by]
Words you missed from my list: [yates]