Java 比较两个以数字作为单词的字符串
我得到的数字是文字:Java 比较两个以数字作为单词的字符串,java,string,numbers,string-comparison,Java,String,Numbers,String Comparison,我得到的数字是文字: {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"}; 数字最多只有10个。我的任务是比较给定的两个输入字符串 当您比较两个数字时,它基本上会起作用: compare(1
{"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;
public int比较(字符串a、字符串b){
返回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 {
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
0
-1
1
可以比较要比较的数字在数组中的位置。 例如,如果输入是“三”和“六”,那么“三”在数组[2]中,“六”在数组[5]中。2<5,意思是“三”<“六”。
其中数组:
String[]数组={“一”、“二”、“三”、“四”、“五”、“六”、“七”、“八”、“九”、“十”}代码>您可以使用映射,只需将数字单词列表与其整数值配对即可。然后,在进行比较时,按照通常的方式比较整数值,而不是实际输入的单词
如果您正在处理大量的可能性,这可能会非常乏味和不切实际,但对于0-10,这是一个合理的解决方案。这里是另一种方法
String s = "onetwothreefourfivesixseveneightnineten";
int compare(String a, String b) {
return Integer.compare(s.indexOf(a),s.indexOf(b));
}
这种方法对我有效
导入java.util.array;
导入java.util.stream.IntStream;
导入java.util.stream.stream;
导入java.util.List;
公共类HelloWorld{
静态字符串[]wordsArray={“一”、“二”、“三”,
“四”,“五”,“六”,“七”,“八”,
“九”、“十”};
公共静态void main(字符串[]args){
//试验
System.out.println(比较(“一”、“一”);
System.out.println(比较(“一”、“三”);
System.out.println(比较(“五”、“二”);
System.out.println(比较(“十”、“四”);
System.out.println(比较(“四”、“十”);
}
公共静态整数比较(字符串a、字符串b){
//生成从1到10的整数数组
int[]myIntArray=IntStream.range(1,wordsArray.length+1)
.toArray();
//转换为字符串数组
字符串[]numbersArray=数组
.toString(myIntArray)
.split(“[\\[\\]]”[1]。split(“,”;
//concat单词数组和数字数组,并将它们转换为字符串列表
List ListWordsNumber=Arrays.asList(
溪流,溪流,溪流,
流量(数字排列))
.toArray(字符串[]::new));
//获取列表中每个参数的索引并添加10个,然后比较索引
返回整数。比较(
整数.parseInt(
获取(listWordsNumbers.indexOf(a)+10)),
整数.parseInt(
get(listWordsNumbers.indexOf(b)+10));
}
}
输出:
0
-1
1
1
-1
您可以使用这些数字的单个列表作为单词,并比较其元素的索引:
static List=Arrays.asList(
“一”,“二”,“三”,“四”,“五”,
“六”、“七”、“八”、“九”、“十”);
静态整数比较(字符串a、字符串b){
返回Integer.compare(list.indexOf(a),list.indexOf(b));
}
publicstaticvoidmain(字符串[]args){
System.out.println(比较(“十”,“一”);//1
System.out.println(比较(“一”、“一”);//0
System.out.println(比较(“一”、“十”);/-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;
};
}
您可以添加分隔符/分隔符以使搜索更精确restrictive@user15244370好的建议,我会把它当作是答案的一部分。
0
-1
1
1
-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;
};
}