Java 我想要一些帮助显示从100万到100万的完整数字

Java 我想要一些帮助显示从100万到100万的完整数字,java,Java,我被分配了一个任务,我不知道该做什么,我一直在写我的程序,已经到了我无法继续下去的地步,我的问题可能很简单,对你们中的一些人来说,但不是真正的我,我需要一种方法来列出从100万到100万的数字,但以文字为例(一二三四……)我找不到一种方法来做这件事,除了单独设置每一个需要很长时间,我能想象的是,你会使用for循环,而不是我只使用System.out.pritnit来显示它。有人能帮我吗 我很多年前就做过类似的事情,很久都不记得是哪种语言了 我想我对一位数和两位数的部分使用了Case语句。有点痛苦

我被分配了一个任务,我不知道该做什么,我一直在写我的程序,已经到了我无法继续下去的地步,我的问题可能很简单,对你们中的一些人来说,但不是真正的我,我需要一种方法来列出从100万到100万的数字,但以文字为例(一二三四……)我找不到一种方法来做这件事,除了单独设置每一个需要很长时间,我能想象的是,你会使用for循环,而不是我只使用System.out.pritnit来显示它。有人能帮我吗

我很多年前就做过类似的事情,很久都不记得是哪种语言了

我想我对一位数和两位数的部分使用了Case语句。有点痛苦,但您只需要编写一次函数

基本上你需要一到十九,这有点奇怪。然后是一个数字,代表你的二十,三十到九十。其余的都很简单,十万,一万,百万

您可以转换为string并获得字符串的长度,然后简单地去掉模3的起始字符。例如,1234是四个字符,因此有数千个,解析掉第一个(4%3=1)字符并在case语句中解码。那么234将是100,得到第一个字符(2)的大小写,剩下34。第一个字符


这是一个很好的,还没有完全清醒(LOL),所以我希望这有助于…

这是另一个给你的,不是最优雅的,但应该给你一些更多的想法

public class WriteNum {

    private static final String[] NUMBERS = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
    private static final String[] TEN_NUMBERS = { "", "ten", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "nintey" };
    private static final String[] TEEN_NUMBERS = { "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen",
            "ninteen" };

    public static void main(String[] args) {
        for (int k = 0; k < 10000; k++) {
            System.out.println(k + "=" + toWords(k));
        }
    }

    private static String toWords(int i) {
        StringBuilder buf = new StringBuilder();
        int hundreds = i % 1000;
        int hundredThousands = (i / 1000) % 1000;

        if (hundredThousands == 0 && hundreds == 0) {
            buf.append("Zero");
        } else {
            if (hundredThousands > 0) {
                writeHundreds(buf, hundredThousands);
                buf.append(" thousand");

                if (hundreds > 0) {
                    buf.append(" ");
                }
            }

            if (hundreds > 0) {
                writeHundreds(buf, hundreds);
            }
        }
        return buf.toString();
    }

    private static void writeHundreds(StringBuilder buf, int hundredThousands) {
        int units = hundredThousands % 10;
        int tens = (hundredThousands / 10) % 10;
        int hundreds = (hundredThousands / 100);

        if (hundreds > 0) {
            buf.append(NUMBERS[hundreds]).append(" hundred");
        }

        boolean teen = false;

        if (tens > 0) {
            buf.append(" and ");
            if (tens == 1) {
                buf.append(TEEN_NUMBERS[units]);
                teen = true;
            } else {
                buf.append(TEN_NUMBERS[tens]);

                if (units > 0) {
                    buf.append(" ");
                }
            }
        }

        if (units > 0 && !teen) {
            if (hundreds > 0 && tens == 0) {
                buf.append(" and ");
            }
            buf.append(NUMBERS[units]);
        }
    }
}
公共类WriteNum{
私有静态最终字符串[]数字={“零”、“一”、“二”、“三”、“四”、“五”、“六”、“七”、“八”、“九”};
私有静态最终字符串[]十个数字={”、“十”、“二十”、“三十”、“四十”、“五十”、“六十”、“七十”、“八十”、“九”};
私有静态最终字符串[]TEEN_NUMBERS={“十”、“十一”、“十二”、“十三”、“十四”、“十五”、“十六”、“十七”、“十八”,
“九天”};
公共静态void main(字符串[]args){
对于(int k=0;k<10000;k++){
System.out.println(k+“=”+toWords(k));
}
}
专用静态字符串toWords(int i){
StringBuilder buf=新的StringBuilder();
整数百分之一百=千分之一;
整数百分之十=(i/1000)%1000;
如果(百分之一百==0和百分之一百==0){
buf.追加(“零”);
}否则{
如果(百分之一百沙>0){
账面价值100(千法郎);
buf.追加(“千”);
如果(百分之一百>0){
buf.追加(“”);
}
}
如果(百分之一百>0){
已登记的数百(基本单位,数百);
}
}
返回buf.toString();
}
私有静态无效写入数(StringBuilder buf,整数百分之一百){
整数单位=百分之十;
整数十位数=(百分之十/10)%10;
整数百=(百分之一百/100);
如果(百分之一百>0){
追加(数字[百])。追加(“百”);
}
布尔值=假;
如果(十位数>0){
buf.附加(“和”);
如果(十=1){
buf.附加(青少年数量[单位]);
青少年=正确;
}否则{
追加(十个数字[十]);
如果(单位>0){
buf.追加(“”);
}
}
}
如果(单位>0&&!个){
如果(百分位>0&&tens==0){
buf.附加(“和”);
}
buf.追加(数字[单位]);
}
}
}

只有0…999999,没有这里的其他解决方案好

您应该将变量除以十分之几百和单位

下面是一些“非常有效的代码”,可能还需要一些调试

import java.util.HashMap;

/**
 * Hello world!
 *
 */
public class App
{

    public static void main( String[] args )
    {
        HashMap<Integer, String> numbers = new HashMap<Integer, String>();
            numbers.put(1, "one");
            numbers.put(2, "two");
            numbers.put(3, "three");
            numbers.put(4, "four");
            numbers.put(5, "five");
            numbers.put(6, "six");
            numbers.put(7, "seven");
            numbers.put(8, "eight");
            numbers.put(9, "nine");
            numbers.put(10, "ten");
            numbers.put(11, "eleven");
            numbers.put(12, "twelve");
            numbers.put(13, "thirteen");
            numbers.put(14, "fourteen");
            numbers.put(15, "fifteen");
            numbers.put(16, "sixteen");
            numbers.put(17, "seventeen");
            numbers.put(18, "eighteen");
            numbers.put(19, "nineteen");

        HashMap<Integer, String> tenths = new HashMap<Integer, String>();
            tenths.put(1, "ten");
            tenths.put(2, "twenty");
            tenths.put(3, "thirty");
            tenths.put(4, "forty");
            tenths.put(5, "fifty");
            tenths.put(6, "sixty");
            tenths.put(7, "seventy");
            tenths.put(8, "eighty");
            tenths.put(9, "ninety");


        for (int i=1; i <= 1000000; i++)
        {
            int million = i/1000000;
            int hundred_thousand = i/100000;
            int tenth_thousand = (i%100000)/10000;
            int unit_thousand = ( tenth_thousand == 1 ? (i%20000)/1000 : (i%10000)/1000);
            int hundred = i%1000/100;
            int tenth = (i%100)/10;
            int unit = (tenth == 1 ? i%20: i%10);
            String output = "".concat((million > 0 ? numbers.get(million).concat(" million ") : ""))
                    .concat(hundred_thousand > 0 ? numbers.get(hundred_thousand).concat(" hundred ") : "")
                    .concat(tenth_thousand > 1 ? tenths.get(tenth_thousand).concat(" ") : "")
                    .concat(unit_thousand > 0 ? numbers.get(unit_thousand).concat(" ") : "")
                    .concat( i >= 1000 ? " thousand " : "")
                    .concat(hundred > 0 ? numbers.get(hundred).concat(" hundred ") : "")
                    .concat(tenth > 1 ? tenths.get(tenth).concat(" ") : "")
                    .concat(unit > 0 ? numbers.get(unit) : "");
            System.out.println(output);

        }
    }
}
import java.util.HashMap;
/**
*你好,世界!
*
*/
公共类应用程序
{
公共静态void main(字符串[]args)
{
HashMap number=新的HashMap();
数字。放入(1,“一”);
数字。放入(2,“2”);
数字。放入(3,“三”);
数字。放入(4,“4”);
数字。放入(5,“5”);
数字。放入(6,“6”);
数字。放入(7,“7”);
数字。放入(8,“8”);
数字。放入(9,“9”);
数字。放入(10,“10”);
数字。放入(11,“11”);
数字。放入(12,“12”);
(13,“十三”);
(14,“14”);
数字。放(15,“15”);
数字。放入(16,“16”);
数字。付诸表决(17,“17”);
(18,“18”);
数字。put(19,“19”);
HashMap tenths=新的HashMap();
十分之一(1,“十”);
十分之一(2,“二十”);
十分之一(3,“三十”);
十分之一(4,“四十”);
十分之一(5,“五十”);
十分之一付诸表决(6,“六十”);
十分之一(7,“七十”);
十分之一(8,“八十”);
十分之一(9,“九十”);
对于(inti=1;i0?number.get(百万)。concat(“百万”):“”)
.concat(十万>0?数字。获取(十万)。concat(“百”):“”)
.concat(十分之一>十分之一)。get(十分之一)。concat(“”):)
.concat(单位:千)大于0?数字.get(单位:千)concat(“”):)
.concat(i>=1000?“千”:“
.concat(百>0?个数字。获取(百)。concat(“百”):“”)
.concat(第十个>十分之一个)。get(第十个)。concat(“”):)
.concat(单位>0?数字。获取(单位):“”);
系统输出打印项次(输出);
}
}
}