将两个字符串与数字作为单词进行比较

Compare two strings with numbers as words

我得到的数字是单词:

{"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"};

数字最多为 10。我的任务是将给定的两个输入字符串相互比较。

当您比较两个数字时,它基本上应该起作用:

compare(1, 1) -> 0;
compare(1, 3) -> 1 < 3 as -1;
compare(5, 2) -> 5 > 2 as 1;

像这样比较两个字符串的最合适方法是什么?

结果看起来像这样:

compare("one", "one") -> 0;
compare("one", "three") -> -1;
compare("five", "two") -> 1;
public int compare(String a, String b) {
    return 0;
}

您可以使用映射来对字符串及其值进行编码。这种方法的好处是它具有 O(1) 复杂性,而不是使用数组。

Map<String, Integer> map = Map.of("one", 1, "two", 2, ...);

public int compare(String a, String b) {
      return Integer.compare(map.get(a),map.get(b));    
}

完整示例:

public class Example {

    private final static Map<String, Integer> STRING_VALUE =
            Map.of("one", 1, "two", 2, "three", 3, "four", 4, "five", 5,
                    "six", 6, "seven", 7, "eight", 8, "nine", 9, "ten", 10);

    public static int compare(String a, String b) {
        return Integer.compare(STRING_VALUE.get(a),STRING_VALUE.get(b));
    }

   public static void main(String[] args) {
       System.out.println(compare("one", "one"));
       System.out.println(compare("one", "three"));
       System.out.println(compare("five", "two"));
    }
}

输出:

0
-1
1

另一种解决方案是使用 ENUM:

完整示例:

public class Example {

    enum Values {
        ONE,
        TWO,
        THREE,
        FOUR,
        FIVE,
        SIX,
        SEVEN,
        EIGHT,
        NINE,
        TEN;
    }
    public static int compare(String a, String b) {
        Values vA = Values.valueOf(a.toUpperCase());
        Values vB = Values.valueOf(b.toUpperCase());
        return Integer.compare(vA.compareTo(vB), 0);
    }

   public static void main(String[] args) {
       System.out.println(compare("one", "one"));
       System.out.println(compare("one", "three"));
       System.out.println(compare("five", "two"));
    }
}

输出:

0
-1
1

你可以比较你要比较的数字在数组中的位置。 例如,如果输入为“三”和“六”,则“三”在数组 [2] 中,“六”在数组 [5] 中。 2 < 5,表示“三” < “六”。 其中数组:String[] array = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"};

您可以使用地图并将数字单词列表与其整数值配对。然后,当您进行比较时,像往常一样比较整数值,而不是实际输入的单词。

如果您要处理大量的可能性,这可能会非常乏味且不切实际,但对于 0-10 种可能性来说,这是一个合理的解决方案。

这是另一种方式。

String s = "onetwothreefourfivesixseveneightnineten";
int compare(String a, String b) {
    return Integer.compare(s.indexOf(a),s.indexOf(b));
}

这个方法对我有用

import java.util.Arrays;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import java.util.List;

public class HelloWorld {
    static String[] wordsArray = {"one", "two", "three",
            "four", "five", "six", "seven", "eight",
            "nine", "ten"};

    public static void main(String[] args) {
        //test
        System.out.println(compare("one", "one"));
        System.out.println(compare("one", "three"));
        System.out.println(compare("five", "two"));
        System.out.println(compare("ten", "four"));
        System.out.println(compare("four", "ten"));
    }

    public static int compare(String a, String b) {
        // generate array of integer from 1 to 10
        int[] myIntArray = IntStream.range(1, wordsArray.length + 1)
                .toArray();

        //convert to string array
        String[] numbersArray = Arrays
                .toString(myIntArray)
                .split("[\[\]]")[1].split(", ");

        //concat word array and number array and convert them to list of string
        List<String> listWordsNumbers = Arrays.asList(
                Stream.concat(Stream.of(wordsArray),
                        Stream.of(numbersArray))
                        .toArray(String[]::new));

        //get index of each parameter in list and add 10 then compare indexes
        return Integer.compare(
                Integer.parseInt(
                        listWordsNumbers.get(listWordsNumbers.indexOf(a) + 10)),
                Integer.parseInt(
                        listWordsNumbers.get(listWordsNumbers.indexOf(b) + 10)));
    }
}

输出:

0
-1
1
1
-1

您可以将这些数字的单个列表用作单词并比较其元素的索引

static List<String> list = Arrays.asList(
        "one", "two", "three", "four", "five",
        "six", "seven", "eight", "nine", "ten");
static int compare(String a, String b) {
    return Integer.compare(list.indexOf(a), list.indexOf(b));
}
public static void main(String[] args) {
    System.out.println(compare("ten", "one")); // 1
    System.out.println(compare("one", "one")); // 0
    System.out.println(compare("one", "ten")); // -1

此外,您可以使用开关:

public static void main(String[] args) throws Exception {
    System.out.println(compare("one", "one")); // 0
    System.out.println(compare("one", "three")); // -1
    System.out.println(compare("five", "two")); // 1
}

public static int compare(String first, String second) {
    return Integer.compare(toInt(first), toInt(second));
}

public static int toInt(String value) {
    return switch (value.toUpperCase()) {
        case "ONE" -> 1;
        case "TWO" -> 2;
        case "THREE" -> 3;
        case "FOUR" -> 4;
        case "FIVE" -> 5;
        case "SIX" -> 6;
        case "SEVEN" -> 7;
        case "EIGHT" -> 8;
        case "NINE" -> 9;
        case "TEN" -> 10;
        default -> 0;
    };
}