有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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


共 (3) 个答案

  1. # 1 楼答案

    你应该把变量除以十分之几百和单位

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

    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);
    
            }
        }
    }
    
  2. # 2 楼答案

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

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

    基本上你需要你的1到19,这是一个有点奇怪的分析。然后一个数字代表你的20,30到90。其余的都很简单,10亿,1亿,1亿

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

    这是预咖啡,还没有完全清醒(哈哈),所以我希望这有帮助

  3. # 3 楼答案

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

    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]);
            }
        }
    }
    

    只有0。。。9999999,不如这里的其他解决方案好