我编写了一个带数字的程序,并用单词显示数字的数字。到目前为止,我的代码从右边开始计算数字。我该如何解决这个问题?
我的代码:
/*/ Write a program that reads an integer and extracts and displays each digit
of the integer in English. So, if the user types in 451,
the program will display four five one /*/
import static java.lang.System.*;
import java.util.*;
class Practice_Problems04_JavaPrograms_Task09{
public static void main(String[] args){
Scanner orcho = new Scanner(in);
String[] myArray = {"zero" , "one" , "two" , "three" , "four" , "five" , "six" , "seven" , "eight" , "nine"};
out.print("Please type your number: ");
double number = orcho.nextDouble();
for(int count = 0; count <= number; count++){
number /= 10.0;
out.print(myArray[(int)(10 * (number - (int)number))] + " ");
}
out.println();
orcho.close();
}
}
答案 0 :(得分:1)
由于这是一项练习而且是出于学习目的,我不会提供任何代码。您应该做的一系列步骤:
double
来获取数字,请坚持使用整数作为问题明确指出that reads an integer
那没关系! 如果你不这样做,就去学习如何做这些步骤!
另一个提示:如果你将输入转换为字符串,则首先将其作为字符串。但你必须检查它是否只是数字对吗? ;)
答案 1 :(得分:1)
为什么要打扰数字呢?你想要这些字符,所以你可以使用你从输入中得到的字符串:
Scanner orcho = new Scanner(System.in);
String[] myArray = {"zero" , "one" , "two" , "three" , "four" , "five" , "six" , "seven" , "eight" , "nine"};
System.out.print("Please type your number: ");
String number = orcho.next();
for (int i=0; i < number.length(); i++) {
System.out.println(myArray[number.charAt(i)-48]);
// -48 because 0 has the ASCII value 48
}