如何将数字转换为文本

阿博法齐

如何编写一个程序,用户输入一个数字作为输入(该数字最多为六位数),然后程序将其转换为文本?

例如,用户输入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]);
乔尔

破裂

您所需要的只是可重用的方法。例如打印1数字号码,2数字号码等的方法...然后在打印第一个数字调用printOneprintTwo如图所示...

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,
    };

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何将数字转换为文本?(在MySQL中)

来自分类Dev

如何将文本列从DBF文件转换为数字

来自分类Dev

如何将数字转换为字母?

来自分类Dev

如何将位数转换为数字

来自分类Dev

如何将数字转换为字符?

来自分类Dev

如何将数字转换为字母?

来自分类Dev

如何将数字转换为unicode?

来自分类Dev

如何将货币转换为数字?

来自分类Dev

如何将时间转换为数字?

来自分类Dev

如何将HTML转换为文本?

来自分类Dev

如何将文本转换为图像?

来自分类Dev

如何将文本转换为 DateTime?

来自分类Dev

如何将文本值转换为Access数字数据类型

来自分类Dev

Excel VBA如何将单元格格式文本转换为数字

来自分类Dev

PHP如何将数字(年份)转换为文本ex。1996 = 196,2004 = 2440

来自分类Dev

如何将值文本框类型=数字转换为十进制

来自分类Dev

C如何将数组中的ASCII数字转换为文本

来自分类Dev

Access VBA-如何将记录集中的值从文本转换为数字?

来自分类Dev

如何将文本和数字数据的混合转换为Apache Spark中的特征数据

来自分类Dev

你如何将 UTF8 数字转换为书面文本

来自分类Dev

如何将非连续数字转换为连续数字?

来自分类Dev

如何将数字转换为3位数字

来自分类Dev

如何将数字列表转换为连续数字列表

来自分类Dev

将数字转换为文本

来自分类Dev

如何将数字始终转换为负数?

来自分类Dev

如何将数字转换为正常日期?

来自分类Dev

Django模板如何将数字转换为单词

来自分类Dev

如何将某些列仅转换为数字?

来自分类Dev

如何将数字数组转换为范围