用文字打印数字

编程入门 行业动态 更新时间:2024-10-26 17:22:13
本文介绍了用文字打印数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用单词打印三位数的代码。 但如果右边的前两位数介于11和11之间,则无效。 19(包括)。

I am trying code to print a three digit number in words. But this isn't working if first two digits from right are between 11 & 19 (both including).

任何帮助?

package com; import java.util.Stack; public class TestMain { public static void main(String[] args) { Integer i=512; int temp =i;int pos=1; Stack<String> stack=new Stack<String>(); while(temp>0){ int rem=temp%10; temp=temp/10; if(rem!=0){stack.push(getString(rem, pos));} pos*=10; } do{ System.out.print(stack.pop()+" "); }while(!stack.isEmpty()); } static String getString(int i,int position){ String str=null; if(position==10){ i*=position; } switch(i){ case 1: str= "One";break; case 2: str= "two";break; case 3: str= "three";break; case 4: str= "four";break; case 5: str= "five";break; case 6: str= "six";break; case 7: str= "seven";break; case 8: str= "eight";break; case 9: str= "nine";break; case 10: str= "Ten";break; case 11: str= "Eleven";break; case 12: str= "Twelve";break; case 13: str= "thirteen";break; case 14: str= "fourteen";break; case 15: str= "fifteen";break; case 16: str= "sixteen";break; case 17: str= "seventeen";break; case 18: str= "eighteen";break; case 19: str= "Nineteen"; break; case 20: str= "twenty";break; case 30: str= "Thirty";break; case 40: str= "forty";break; case 50: str= "Fifty";break; case 60: str= "sixty";break; case 70: str= "Seventy";break; case 80: str= "Eighty"; break; case 90: str= "Ninety";break; case 100: str= "Hundred"; break; } if(position>=100){ str=str+ " "+getString(position, 0); } return str; }

}

推荐答案

对于三位数字(负数或正数),您只需要将其分成几个部分,并记住小于20的数字是特殊的(在任何一百个数字中)。作为伪代码(嗯,Python,实际上但足够接近伪代码),我们首先定义查找表:

For a three-digit number (negative or positive), you just need to divide it into sections and remember that numbers less than 20 are special (in any block of a hundred). As pseudo-code (well, Python, actually but close enough to pseudo-code), we first define the lookup tables:

nums1 = ["","one","two","three","four","five","six","seven", "eight","nine","ten","eleven","twelve","thirteen", "fourteen","fifteen","sixteen","seventeen","eighteen", "nineteen"] nums2 = ["","","twenty","thirty","forty","fifty","sixty", "seventy","eighty","ninety"]

请注意,对于10到19之间的数字,没有 onety 。如前所述,每百个块中的二十个以下的数字是专门处理的。

Note that there's no onety for numbers between ten and nineteen. As previously mentioned, numbers under twenty in each block of a hundred are treated specially.

然后我们有一个函数,它首先检查输入值并处理负数作为一个级递归调用。它还处理零的特殊情况:

Then we have the function which first checks the input value and handles negative numbers as a one-level recursive call. It also handles the special case of zero:

def speak (n): if n < -999 or n > 999: return "out of range" if n < 0: return "negative " + speak (-n) if n == 0: return "zero"

下一步是算出数字中的三位数:

Next step is to work out the three digits in the number:

hundreds = int (n / 100) tens = int ((n % 100) / 10) ones = n % 10

数百个很简单,因为它只有零到九:

Hundreds are simple since it's only zero through nine:

if hundreds > 0: retstr = nums1[hundreds] + " hundred" if tens != 0 or ones != 0: retstr = retstr + " and " else: retstr = ""

其余的只是将零到十九的值视为特殊值,否则我们将其视为 X ty Y (如四十二或七十七):

The rest is simply treating values from zero to nineteen as special, otherwise we treat it as Xty Y (like forty two or seventy seven):

if tens != 0 or ones != 0: if tens < 2: retstr = retstr + nums1[tens*10+ones] else: retstr = retstr + nums2[tens] if ones != 0: retstr = retstr + " " + nums1[ones] return retstr

快速和底部的脏测试套件:

And a quick and dirty test suite at the bottom:

for i in range (-1000, 1001): print "%5d %s"%(i, speak (i))

产生:

-1000 out of range -999 negative nine hundred and ninety nine -998 negative nine hundred and ninety eight : -2 negative two -1 negative one 0 zero 1 one 2 two : 10 ten 11 eleven 12 twelve : 998 nine hundred and ninety eight 999 nine hundred and ninety nine 1000 out of range

更多推荐

用文字打印数字

本文发布于:2023-08-07 06:57:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1317615.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文字   数字

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!