小编The*_*irl的帖子

用数字作为单词比较两个字符串

我得到了数字作为单词:

{"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"};
Run Code Online (Sandbox Code Playgroud)

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

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

compare(1, 1) -> 0;
compare(1, 3) -> 1 < 3 as -1;
compare(5, 2) -> 5 > 2 as 1;
Run Code Online (Sandbox Code Playgroud)

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

结果看起来像这样:

compare("one", "one") -> 0;
compare("one", "three") -> -1;
compare("five", "two") -> 1;
Run Code Online (Sandbox Code Playgroud)
{"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"};
Run Code Online (Sandbox Code Playgroud)

java string numbers string-comparison

4
推荐指数
2
解决办法
219
查看次数

标签 统计

java ×1

numbers ×1

string ×1

string-comparison ×1