我已经编写了一个程序,该程序需要一个数字,并以单词显示数字的数字。到目前为止,我的代码从右边开始计数。我怎样才能解决这个问题?

我的代码:

  /*/  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();
     }
 }

最佳答案

由于这是一个练习,并且仅用于学习目的,因此我将不提供任何代码。只需执行以下步骤即可:


您已经有一个带有数字名称的数组,这很好
不要使用double来获取数字,而是使用整数,因为问题明确指出了that reads an integer
将其转换为字符串
循环字符串的每个字符
再次将每个字符转换为整数(在循环内)´
只需从您的姓名数字数组中打印该位置即可。提示:使用一种不会为每次打印创建新行的方法:)


那就好了!
如果不这样做,那就去学习如何做这些步骤吧!

另一个提示:如果您要将输入转换为字符串,只需先将其获取为字符串即可。但是您将不得不检查是否只有数字正确? ;)

关于java - 将数字转换成单词,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37364097/

10-11 22:30
查看更多