java程序将数字转换为字符串

时间:2014-02-05 12:31:32

标签: java

如何为100到999之间的数字输入创建一个回报。

 public class numbers {
        static String[] ones={" ","one","two","three","four","five","six","seven","eight","nine","ten",
            "eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
        static String [] tens={" ","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"};
        static String [] hundreds={" ","one-hundred","two-hundred","three-hundred","four-hundred",
            "fife-hundred","six-hundred","sevent-hundred","eight-hundred","nine-hundred"};
         static String[] thousand= {"Thousand"};

    public static String UpTONineteen(int i){
        Scanner t=new Scanner(System.in);
        System.out.print("please enter a number\n");
        i=t.nextInt();
         while(i>0 && i<20){
             return ones[i];
         }
         while(i>=20 && i<100){
             return tens[i/10]+ones[i%10];
         }while(i>=100 && i<1000){

         }

        return "";

    }

    public static void main(String []args){ 
        numbers b=new numbers();
        System.out.print(b.UpTONineteen(10));

    }

    }

4 个答案:

答案 0 :(得分:1)

首先:你有一个十几个数组的问题,它需要是这样的,因为你使用索引

static String[] tens = {" ", " ", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"};

第二:百范围

return hundreds[i / 100] +" "+ tens[(i % 100) / 10] +" "+ ones[i % 10];

答案 1 :(得分:0)

我认为,以下代码应该有效:

return hundreds[i / 100] + tens[(i % 100) / 10] + ones[i % 10];

答案 2 :(得分:0)

试试这个:

while(i>=100 && i<1000){
    return hundreds[i / 100] + tens[Math.max((i % 100) / 10-1,0)] + ones[i % 10];
}

答案 3 :(得分:-2)

String.valueOf()是你的朋友。搜索如何使用它;)
examples