如何转换int以便返回字符串,这就是我们说int的方式 [英] How to convert an int so as to return the string which is how we say the int

查看:122
本文介绍了如何转换int以便返回字符串,这就是我们说int的方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了这个面试问题,面试官要求写一个函数,它会取一个整数,比如说123,并且会返回一个字符串,就像人们会说整数一样。在上面的情况下输出应该是百我不知道如何解决这些问题。任何人都可以帮忙解决这个问题吗?伪代码将是有用的。

I came across this interview question where the interviewer asked to write a function which will take an integer, say 123, and will return a string that would be how one would say that integer.In above case output should be "one hundred and twenty three".I have no idea how to solve these problems.Can anyone help in how to work out this problem? A pseudo code will be helpfull.

推荐答案

使用此代码将数字转换为Word源这里

Use this Code for Converting Numbers to Word Source here

 public class YourNumberMyWord { 
        public void pw(int n,String ch) { 
            String one[]={" "," one"," two"," three"," four"," five"," " +
                    "six"," seven"," eight"," Nine"," ten"," eleven"," twelve"," " +
                            "thirteen"," fourteen","fifteen"," sixteen"," seventeen"," eighteen"," nineteen"}; 

            String ten[]={" "," "," twenty"," thirty"," forty"," fifty"," sixty","seventy"," eighty"," ninety"};   
            if(n>19) { 
                System.out.print(ten[n/10]+" "+one[n%10]);
            } 
            else { 
                System.out.print(one[n]);} if(n>0)System.out.print(ch); 
                } 

        public static void main(String[] args) {
            int n=0; Scanner scanf = new Scanner(System.in); 
            System.out.println("Enter an integer number: "); 
            n = scanf.nextInt();

            if(n<=0) 
                System.out.println("Enter numbers greater than 0"); 
            else { 
                YourNumberMyWord a = new YourNumberMyWord(); 
                a.pw((n/1000000000)," Hundred"); a.pw((n/10000000)%100," crore"); 
                a.pw(((n/100000)%100)," lakh"); a.pw(((n/1000)%100)," thousand"); 
                a.pw(((n/100)%10)," hundred"); a.pw((n%100)," "); 
            }
            }

    }

这篇关于如何转换int以便返回字符串,这就是我们说int的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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