将答案(数字)从之前的计算转换为单词。 JAVA

时间:2014-07-14 07:33:30

标签: java

Scanner kamote = new Scanner (System.in);
    int year, thousands, hundreds, tens, ones;

    System.out.println ("Enter Year: ");
    year = kamote.nextInt();

    thousands = year / 1000 * 1000;
    System.out.println ("Thousands: " +thousands );

    hundreds = ((year/100)*100) - thousands;
    System.out.println("Hundreds: " +hundreds);

    tens = ((year/10)*10) - thousands - hundreds;
    System.out.println("Tens: " +tens);

    ones = year - thousands - hundreds - tens;
    System.out.println ("Ones: " + ones);  
}

上面分别显示数千到1的小数位,但我的导师要求更复杂的小数位。我们需要将数字(答案,例如1000)转换为单词(一千)。他说我们需要在这里使用switch语句。

1 个答案:

答案 0 :(得分:-1)

只是一个想法:

int ones = year%10;
int tens = year;
while(tens/10>0)
{
   tens = tens/10;
}
switch(tens)
{
    case 1:
        switch(ones):
            case 1:
                String tensS = eleven;
                break;
            case 2:
                String tensS = twelve;
                break;
            // Ando so on...
        break;
    case 2:
        String tensS = "twenty " + getOnes(ones);
        break;
    case 3:
        String tensS = "thirty " + getOnes(ones);
        break;
    // and so on
}



public String getOnes(int ones)
{
    switch(ones):
    {
        case 1:
            return "one";
        case 2:
            return "two";
        // and so on
    }
}

哟必须使用更多的数千和数百个交换机块,然后连接整个字符串