0

我想置换键盘输入的单词并与列表的元素进行比较。我如何使用 groovy 来做到这一点?

4

1 回答 1

0

对,猜一猜你的意思,这可以让你输入一行文本。该行文本被拆分成单词,然后打印出来;

  1. 在内部列表中找到的输入词
  2. 输入的单词不在内部列表中
  3. 内部列表中未输入的单词

这是代码:

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]
于 2012-04-25T08:03:34.440 回答