问题
解释
输入一个数字,需要我们将每一位的数字相加得到和,将和的每一位数字从高位开始,翻译成英文
解题思路
题目中表示输入的数字<=,是一个非常大的数字,所以我们不能用数值型变量保存,需要用字符串型接收。遍历每一位求和得到的数字,再翻译成英文就行了,注意格式!!
代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String N=sc.next();
char[] arr=N.toCharArray();
int res=0;
for (char c : arr) {
res+=c-'0';
}
String result=res+"";
switch (result.charAt(0)){
case '0':
System.out.print("zero");
break;
case '1':
System.out.print("one");
break;
case '2':
System.out.print("two");
break;
case '3':
System.out.print("three");
break;
case '4':
System.out.print("four");
break;
case '5':
System.out.print("five");
break;
case '6':
System.out.print("six");
break;
case '7':
System.out.print("seven");
break;
case '8':
System.out.print("eight");
break;
case '9':
System.out.print("nine");
break;
}
for (int i = 1; i < result.length(); i++) {
switch (result.charAt(i)){
case '0':
System.out.print(" zero");
break;
case '1':
System.out.print(" one");
break;
case '2':
System.out.print(" two");
break;
case '3':
System.out.print(" three");
break;
case '4':
System.out.print(" four");
break;
case '5':
System.out.print(" five");
break;
case '6':
System.out.print(" six");
break;
case '7':
System.out.print(" seven");
break;
case '8':
System.out.print(" eight");
break;
case '9':
System.out.print(" nine");
break;
}
}
}
}