首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将数字转换为单词的程序

将数字转换为单词的程序
EN

Stack Overflow用户
提问于 2016-10-02 03:03:26
回答 1查看 4.6K关注 0票数 0

我能找人帮忙吗?我是编程新手。我开始编程已经有一周了。我一直在做一个程序,它需要输入一个数字,并输出你输入的数字的字母。我的问题是。这个程序有没有替代的代码?另外,如果输入为-1,它将读取为“负1”,我如何添加一些代码?我真的很感谢你们能给我的帮助。

这是我一直在做的代码:

示例输出为:输入数字: 12345输出:一万二千三百四十五

代码语言:javascript
复制
import java.util.Scanner;

public class Numberconvert {

    public boolean Number(String s) {
        boolean tama = true;
        for (int i = 0; i < s.length(); i++) {
            if (!Character.isDigit(s.charAt(i))) {
                return false;
            }
        }

        return tama;
    }

    public static String convert(int digit) {
        String ArrayOnes[] = {"zero", " One ", " Two ", " Three ", " Four ", " Five ", " Six ", " Seven ", " Eight ", " Nine ", " Ten ",
             " Eleven ", " Twleve ", " Thirteen ", " Fourteen ", " Fifteen ", " Sixteen ", " Seventeen ", " Eighteen ", " Ninteen "};
        String ArrayOnes2[] = {" ", " Ten ", " Twenty ", " Thirty ", " Fourty ", " Fifty ", " Sixty ", " Seventy ", " Eighty ", " Ninety "};
        String ArrayNegative[] = {"Negative "};

        if (digit < 0) {
            return ArrayNegative[0];
        }
        if (digit % 10 == 0) {
            ArrayOnes[0] = "";
        }
        if (digit < 20) {
            return ArrayOnes[digit];
        }
        if (digit < 100) {
            return ArrayOnes2[digit / 10] + ArrayOnes[digit % 10];
        }
        if (digit < 10000) //return convert(digit/100)+" Hundred "+convert(digit %100);
        {
            return convert(digit / 100) + " Thousand " + convert(digit % 100);
        }
        if (digit < 100000000) {
            return convert(digit / 1000) + " Thousand " + convert(digit % 1000);
        }
        return convert(digit / 1000000000) + " Million " + convert(digit % 1000000000);
        //return convert(digit);
    }

    public static void main(String[] args) {
        Scanner scann = new Scanner(System.in);
        String Number;

        // checker - class to loop until the input is number
        Numberconvert checkIfNumber = new Numberconvert();

        // convert string in to number
        int result;

        try {
            // this do while method is only to check if the input is Number 
            do {
                System.out.print("Enter a Number: ");
                Number = scann.nextLine();
            } while (!checkIfNumber.Number(Number));

            // this if statement is only works if the input is Number
            if (checkIfNumber.Number(Number)) {
                // String into int to process the String
                result = Integer.parseInt(Number);
                // initialize "converted" to the function;
                String converted = convert(result);
                // print the result
                System.out.println(converted);
            }

            // if ever some error detected. The user want to input again
        } catch (Exception e) {
            System.out.println("");
            System.err.println("Some Errors Detected ");
            System.out.println("Want to input again? Press Enter ");
            scann.nextLine();
            main(null);
        }
    }

}
EN

回答 1

Stack Overflow用户

发布于 2016-10-02 03:14:54

这个问题已经在here中被问到了

并且有一个link to the sourcecode (与答案1相同)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39810423

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档