gpt4 book ai didi

java - 将数字的数字转换为单词

转载 作者:行者123 更新时间:2023-12-02 03:36:48 25 4
gpt4 key购买 nike

我编写了一个程序,它接受一个数字并用文字显示该数字的数字。到目前为止,我的代码从右边开始计算数字。我该如何解决这个问题?

我的代码:

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

最佳答案

由于这是一个练习并且是出于学习目的,因此我不会提供任何代码。只是您应该执行的一组步骤:

  1. 您已经有一个包含数字名称的数组,这很好
  2. 不要使用double来获取数字,坚持使用整数,因为问题明确指出它读取整数
  3. 将其转换为字符串
  4. 循环字符串的每个字符
  5. 再次将每个字符转换为整数(在循环内)´
  6. 只需打印您的姓名数字数组中的位置即可。提示:使用一种不为每次打印创建新行的方法:)

那就太好了!
如果您不会,请学习如何执行这些步骤!

另一个提示:如果您要将输入转换为字符串,只需首先将其作为字符串获取。但你必须检查它是否只是数字,对吧? ;)

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com