将数字转换为单词 [英] Converting number to word

查看:85
本文介绍了将数字转换为单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得一些将整数转换为单词的帮助.我对Java还是很陌生,我对自己的想法有所了解,但需要一些帮助.

I would like to get some help on converting an integer to word. I am very new to Java, i kind of have an idea of what i think im going to be doing but need some help with it.

该代码应打印0-999之间的数字,一旦在扫描仪中键入-1,程序应停止.

The code should print the words of number from 0-999 and once -1 typed in the scanner the program should stop.

赞:

请输入0到999之间的数字:1

Please type a number between 0 and 999: 1

一个

请输入0到999之间的数字:11

Please type a number between 0 and 999: 11

偶像

请输入0到999之间的数字:122

Please type a number between 0 and 999: 122

一百二十个

请输入0到999之间的数字:1000

Please type a number between 0 and 999: 1000

数量超出范围

请输入0到999之间的数字:-1

Please type a number between 0 and 999: -1

感谢您使用我们的程序

我还必须使用方法来制作此清洁器"

I also have to use methods to make this "Cleaner"

推荐答案

首先,由于 number,所以用100除以百位并通过调用 numberToWord((number / 100), " HUNDRED"); 方法打印相应的数字/100 介于0到9之间,因此它将在HUNDRED串联的单词中打印数字.

First of all take hundreds place digit by deviding by 100 and print corresponding number by calling method numberToWord((number / 100), " HUNDRED"); since number / 100 would be in between 0 to 9 so it will print digit in word concatenated by HUNDRED.

现在,您留下了两位数的数字,您可以直接呼叫 numberToWord((number % 100), " "); ,因为我们将数字取100的模,因此它只能传递两位数. if (num > 19) {System.out.print(tens[num / 10] + " " + ones[num % 10]);} ,它将占据数十位并打印由一个连在一起的数十个单词. else {System.out.print(ones[num]);} 直接从数组中打印1到19之间的单词.

Now you left with two digit number for that you directly call numberToWord((number % 100), " "); since we are taking modulo 100 of number so it would pass two digit only. if (num > 19) {System.out.print(tens[num / 10] + " " + ones[num % 10]);} then it will take tens place and print tens word concatenated by ones. else {System.out.print(ones[num]);} directly prints word between 1 to 19 from the array.

import java.util.Scanner;

public class Test1 {



        public static void main(String[] args) {
            int number = 0;
            Scanner scanner = new Scanner(System.in);
            System.out.print("Please type a number between 0 and 999 OR type -1 to exit:  ");
            number = scanner.nextInt();
            while(number!=-1){
                if(number>=0 && number<=999){
                    if(number==0){
                        System.out.print("NUMBER AFTER CONVERSION:\tZERO");
                    } else {
                        System.out.print("NUMBER AFTER CONVERSION:\t");
                        numberToWord(((number / 100) % 10), " HUNDRED");
                        numberToWord((number % 100), " ");
                    }

                } else{
                    System.out.print("NUMBER OUT OF RANGE");
                }
                System.out.print("\nPlease type a number between 0 and 999 OR type -1 to exit:  ");
                number = scanner.nextInt();
            }
        }

        public static void numberToWord(int num, String val) {
            String ones[] = {" ", " ONE", " TWO", " THREE", " FOUR", " FIVE", " SIX", " SEVEN", " EIGHT", " NINE", " TEN", " ELEVEN", " TWELVE", " THIRTEEN", " FOURTEEN", " FIFTEEN", " SIXTEEN", " SEVENTEEN", " EIGHTEEN", " NINETEEN"
            };
            String tens[] = {" ", " ", " TWENTY", " THIRTY", " FOURTY", " FIFTY", " SIXTY", " SEVENTY", " EIGHTY", " NINETY"};
            if (num > 19) {
                System.out.print(tens[num / 10] + " " + ones[num % 10]);
            } else {
                System.out.print(ones[num]);
            }
            if (num > 0) {
                System.out.print(val);
            }
        }
    }

示例输出:

Please type a number between 0 and 999 OR type -1 to exit:  563
NUMBER AFTER CONVERSION:     FIVE HUNDRED SIXTY  THREE 
Please type a number between 0 and 999 OR type -1 to exit:  45
NUMBER AFTER CONVERSION:      FOURTY  FIVE 
Please type a number between 0 and 999 OR type -1 to exit:  6 
NUMBER AFTER CONVERSION:      SIX 
Please type a number between 0 and 999 OR type -1 to exit:  0
NUMBER AFTER CONVERSION:    ZERO 
Please type a number between 0 and 999 OR type -1 to exit:  -1
exit

这篇关于将数字转换为单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆