[英]Method that receives integer and returns in string
I have an exercice to do but i cant seem to do a thing.我有一项运动要做,但我似乎无能为力。 We have to transform an integer that the user chooses and then convert it to string, for ex: input = 2409 output = two, four, zero, nine
我们必须转换用户选择的整数,然后将其转换为字符串,例如:输入 = 2409 输出 = 二、四、零、九
if someone could help i would be very appreciated如果有人可以提供帮助,我将不胜感激
i can do only if the input has only one digits, but cant do it with multiple digits.我只能在输入只有一位数字时做,但不能用多位数字做。
First create your hashmap
describing the mapping of digits to words.首先创建描述数字到单词映射的
hashmap
图。 The rest is simple comparison:剩下的就是简单的比较:
public static void printDigitsAsWords(int n) {
String s = String.valueOf(n);
String[] digits = s.split("");
Map<String, String> map = Map.of(
"0", "zero",
"1", "one",
"2", "two",
"3", "three",
"4", "four",
"5", "five",
"6", "six",
"7", "seven",
"8", "eight",
"9", "nine"
);
String result = Arrays.stream(digits)
.map(digit -> map.get(digit))
.collect(Collectors.joining(", "));
System.out.println(result);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.