如何为输入排列单词

时间:2012-04-24 18:57:23

标签: groovy

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

1 个答案:

答案 0 :(得分:0)

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

  1. 输入了内部列表中的字词
  2. 输入了不在内部列表中的单词
  3. 内部列表中未输入的字词
  4. 以下是代码:

    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]