如何转换int以便返回字符串,这就是我们说int的方式

时间:2014-02-03 07:05:41

标签: java string int

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

5 个答案:

答案 0 :(得分:4)

以下是您问题的解决方案:

Numbers to words

这并不像你想象的那么简单。

答案 1 :(得分:2)

使用此代码将数字转换为Word来源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)," "); 
            }
            }

    }

答案 2 :(得分:0)

使用前面编写的相同convertLessThanOneThousand函数。

private static String pronounce(int number) {
            String result = "";
            int cnt = 0;
            while (number != 0) {
                int rem = number % 1000;
                result = convertLessThanOneThousand(rem) + result;
                number = number / 1000;
                cnt++;
                if (number != 0) {
                    switch (cnt) {
                    case 1:
                        result = " thousand " + result;
                        break;
                    case 2:
                        result = " million " + result;
                        break;
                    case 3:
                        result = " billion " + result;
                        break;
                    }
                }
            }
            return result;
        }

答案 3 :(得分:0)

这是我将int转换为英文单词的代码。希望它有所帮助。

public class Excercise1 {

    static String words[]={"One","Two","Three","Four","Five", "Six", "Seven", "Eight", "Nine", "Ten"};

    public static void main(String[] args){

        String inputValue = JOptionPane.showInputDialog("Please input a value");

        int num = Integer.parseInt(inputValue.toString());
        if(num <= 10){
            System.out.println(words[num-1]);
        } else {
            JOptionPane.showMessageDialog(null, "You must put number 1 - 10", "alert",  JOptionPane.ERROR_MESSAGE);
        }

    }

}

答案 4 :(得分:-5)

现在有很多方法......现在

1.String.valueOf(intVarable)

2.Integer.toString(intVarable)

public class ConvertIntToString {
  public static void main(String[] args) {

    int aInt = 1;

    String aString = Integer.toString(aInt);

  }
}