1

请原谅我的格式错误,因为我在这里很新。我有一个 Java 作业,我必须在其中找到大于来自扫描仪的给定单词的词典最低单词,而无需将字符串按词典顺序排列。在这个例子中,给定的单词是“am”。我已经编写了下面的代码,但我有一些问题,我将在这个问题的末尾解释。

    public static void main (String[] args){
    Scanner s = new Scanner("I am very happy with life");

    String high = "";
    String o = "am";
    String curr = "";

    while (s.hasNext()){
        curr = s.next();
        if (curr.compareTo(high) > 0)
            high = curr;
    }

    Scanner ss = new Scanner("I am very happy with life");

    while (ss.hasNext()){
        curr = ss.next();
        if (curr.compareTo(high) < 0 && curr.compareTo(o) > 0)
            high = curr;
    }

    System.out.println(high);
}}

我的问题是:我必须编写一个进行计算的方法,而不是在 main 中使用该过程。此外,我必须使用一次扫描仪,我无法用相同的值初始化另一个扫描仪。

此代码工作正常,但我最难将其转换为单个功能循环方法。

PS。抱歉愚蠢的变量名称。

4

3 回答 3

2

像这样的东西(使用 a TreeSet):

import java.util.Scanner;
import java.util.TreeSet;

public class LexicographicScanner {

    private final TreeSet<String> words = new TreeSet<String>();

    public LexicographicScanner( final Scanner scanner )
    {
        while ( scanner.hasNext() )
        {
            words.add( scanner.next() );
        }
        scanner.close();
    }

    public String nextWord( final String word )
    {
        return words.higher( word );
    }

    public static void main(String[] args) {
        final LexicographicScanner ls
            = new LexicographicScanner ( new Scanner("I am very happy with life") );

        System.out.println( ls.nextWord( "am" ) );
        System.out.println( ls.nextWord( "I" ) );
        System.out.println( ls.nextWord( "with" ) );
    }
}

输出

happy
am
null

编辑

没有TreeSet

public class LexicographicScanner {

    public static String nextWord( final Scanner scanner, final String word )
    {
        String higher = null, curr;
        while ( scanner.hasNext() )
        {
            curr = scanner.next();
            if ( curr.compareTo( word ) > 0 )
            {
                if ( higher == null || curr.compareTo( higher ) < 0 )
                    higher = curr;
            }
        }
        return higher;
    }

    public static void main(String[] args) {
        final Scanner s1 = new Scanner("I am very happy with life");
        final Scanner s2 = new Scanner("I am very happy with life");
        final Scanner s3 = new Scanner("I am very happy with life");
        System.out.println( nextWord( s1, "am" ) );
        System.out.println( nextWord( s2, "I" ) );
        System.out.println( nextWord( s3, "with" ) );
        s1.close();
        s2.close();
        s3.close();
    }
}
于 2014-02-12T10:23:42.913 回答
2

首先提出一些要求:

  1. 我要找的词的字典顺序比我输入的词高
  2. 我正在寻找的词是词典最低的可能

所以基本上你必须逐字检查输入的句子并检查这两个要求。这是一些伪代码,我希望这有助于理解您的问题/此解决方案。

inputWord <= input
currentlyHighest <= null

for (word <= sentence) {
    is word higher than inputWord?
        no: discard word and analyze the next one
        yes: go on

    do i have a currently highest word?
        no: save the word in currentlyHighest and analyze the next word
        yes: go on

    is word lower than currentlyHighest?
        no: discard word and analyze the next one
        yes: we have found a better match: save word in currentlyHighest and analyze the next one
}
于 2014-02-12T11:09:56.550 回答
1

暗示:

String res = null;
Scanner s = new Scanner(str);
for each curr in s scanner {
    if (curr greater than given
            and (dont have res or curr is less than res)) {
        res = curr;
    }
}
于 2014-02-12T11:01:34.053 回答