如何将数字转换为文本[重复]
Posted
技术标签:
【中文标题】如何将数字转换为文本[重复]【英文标题】:How to convert a number to text [duplicate] 【发布时间】:2021-05-04 07:33:36 【问题描述】:如何编写一个程序,用户输入一个数字作为输入(数字最多可以是六位数字),然后程序将其转换为文本?
例如,用户输入 100 并打印输出“一百”。
代码必须使用 Java。用户最多可以随机输入六位数字
我写了一个代码如下,但是是两位数的,对于更多的位数,我一直在写它的方式
public static void main(String args[])
Scanner sc=new Scanner(System.in);
System.out.println("enter the number");
int n=sc.nextInt();
int n1=n,n2=n;
int b=n1%10,a=n2/10; // n1/10 means last digit is removed and n2%10 means last digit by modulus
String[] single_digits = new String[]"zero","one","two","three","four","five", "six","seven","eight","nine";
String[] two_digits = new String[]"","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen", "nineteen";
String[] tens_multiple = new String[]"","","twenty","thirty","forty","fifty","sixty", "seventy","eighty","ninety";
if(a==1)
System.out.println(two_digits[b+1]);
else if(b==0)
System.out.println(tens_multiple[a]);
else
System.out.println(tens_multiple[a]+"-"+single_digits[b]);
【问题讨论】:
你有没有尝试过?你到底卡在哪里了? 我最多可以写两位数,但我不能写超过两位数 100 有什么不同?你最多可以解决99,对吧?请编辑您的问题以包含您的现有代码 【参考方案1】:破解
您所需要的只是可重用的方法。例如打印1
数字、2
数字等的方法...然后您在打印第一个之后在printTwo
中调用printOne
,如图所示...
import java.util.Scanner;
public class x
// Instance fields are below....
// Placed below for easy readability of code...
public static void main (String args[])
// You know what this does...
System.out.print (" Input number\n > ");
Scanner sc = new Scanner (System.in);
// Create int array to collect all digits...
String number = Integer.toString (sc.nextInt ());
int[] digits = new int[number.length ()];
for (int i = 0; i < number.length (); i++)
digits [i] = Integer.parseInt (number.charAt (i) + "");
// All you need is a simple method to print a given amount..even up to ten million
switch (digits.length)
case 1:
printOne (digits);
break;
case 2:
printTwo (digits);
break;
case 3:
printThree (digits);
break;
case 4:
printFour (digits);
break;
case 5:
printFive (digits);
break;
case 6:
printSix (digits);
break;
static void printOne (int[] arr)
// Just print coresponding value, easy since i converted to int[]
System.out.print (below_ten [arr [0]]);
static void printTwo (int[] arr)
// check if it is a ~teen/not (you know what i mean)
if (arr [0] == 1)
System.out.print (below_twenty [arr [1] + 1]);
else
System.out.print (below_hundered [arr [0]] + " ");
if (arr [1] != 0) // added this to prevent 30 = thirty zero
printOne (new int[] arr [1]); // this wont be needed for the ~teens
static void printThree (int[] arr)
// now everything is siple, just print first digits and call previous, to call previous...
// you get where that goes...
System.out.print (below_thousand [arr [0]] + " ");
printTwo (new int[] arr [1], arr [2]);
// Now you have the algorithm, Challenge=print up to ten million... :-)
static void printFour (int[] arr)
// challenge
static void printFive (int[] arr)
//exercise
static void printSix (int[] arr)
//bounty :-b)
// Placed below for easy readability of code...
static String hundred = " hundred";
static String[] below_ten = new String[]
"zero","one","two","three","four","five", "six","seven","eight","nine"
;
static String[] below_twenty = new String[]
"","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen", "nineteen"
;
static String[] below_hundered = new String[]
"","","twenty","thirty","forty","fifty","sixty", "seventy","eighty","ninety"
;
static String[] below_thousand = new String[]
"", below_ten [1] + hundred, below_ten [2] + hundred, below_ten [3] + hundred, below_ten [4] + hundred, below_ten [5] + hundred, below_ten [6] + hundred, below_ten [7] + hundred, below_ten [8] + hundred, below_ten [9] + hundred,
;
_祝你好运...
编辑 万一您在使用其他方法时遇到问题......比魅力更有效......
static void printFour (int[] arr)
System.out.print (below_million [arr [0]]);
printThree (new int[] arr [1], arr [2], arr [3]);
static void printFive (int[] arr)
printTwo (new int[] arr [0], arr [1]);
System.out.print (thousand);
printThree (new int[] arr [2], arr [3], arr [4]);
static void printSix (int[] arr)
printThree (new int[] arr [0], arr [1], arr [2]);
System.out.print (thousand);
printThree (new int[] arr [3], arr [4], arr [5]);
static String thousand = " thousand ";
static String[] below_million = new String[]
"", below_ten [1] + thousand, below_ten [2] + thousand, below_ten [3] + thousand, below_ten [4] + thousand, below_ten [5] + thousand, below_ten [6] + thousand, below_ten [7] + thousand, below_ten [8] + thousand, below_ten [9] + thousand,
;
【讨论】:
非常感谢您的帮助但是代码在以下部分给出了错误:静态字符串千=“千”; static String[] below_million = new String[] "", below_ten [1] +千, below_1 [2] +千, below_1 [3] +千, below_1 [4] +千, below_1 [5] +千, below_1 [6] + 千,低于 10 [7] + 千,低于 10 [8] + 千,低于 10 [9] + 千,;错误:在初始化期间使用静态非最终变量。如何修复错误? @Abolfazl 好的,这只是一个玩具程序,获取原始的 here 以及所有说明,并且没有限制。 非常感谢,我的问题已经解决了。以上是关于如何将数字转换为文本[重复]的主要内容,如果未能解决你的问题,请参考以下文章
将 Pandas 系列日期时间“月份”数字转换为月份文本 [重复]