2 exit door eXit_door 于 2013.07.17 11:25 提问

转换数字为指示性字符

我从数据库中得到的数据是1,2,3这样的格式,我想在标签中显示为one,two这样,应该怎么实现?

谢谢

3个回答

lmcptbtptp
lmcptbtptp   2013.07.17 11:29
已采纳

换一下就行,

public static void main(String[] args)
{
//这里有定义成一个数组...这样才方便
String x[]={"zero","one","two", "three","four", "five","six","seven","eight","nine",
"ten","eleven","twelve","thirteen","fourteen","fifteen", "sixteen","seventeen","eighteen","nineteen",
"twenty","thirty","fourty","fifty", "sixty","seventy", "eighty","ninety"
} ;

Scanner in = new Scanner(System.in);
String s;
s = in.next();//这里用 next()方法
int j=0;

for(int i=0 ; i<x.length; i++ )
{
if(s==x[0])
{
//这里判断 如果输入的字符串有和你数组里定义的字符串有相同的 就跳出
//需要注意的是数组的索引是从0开始的所以对应的数组要+1
j=i+1;
}
}
System.out.println("你输入的数字是"+j);

}

zhanglei5415
zhanglei5415   Rxr 2013.07.17 14:33

如果全部是10以内的数,是比较好处理。最笨的方法就是定义一个数组来存储英文串

-(NSString *)convertToEnglish:(int) num {
   NSArray *array=@{@"zero",@"one",@"two",@"three",@"four",@"five",@"six",@"seven",@"eight",@"nine"};
/////得到数字是几,就用几当下标索引去数组中查找就可以了
   NSLog(@"get english is :%@",[array objectAtIndex:num]);
}

如果是大于10的就要另写算法来处理了

Chengzi_963
Chengzi_963   2013.07.17 15:55
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle: NSNumberFormatterSpellOutStyle];
NSString* numberString = [formatter stringFromNumber:[NSNumber numberWithInt:3]];
NSLog(@"%@",numberString);

试试这样替换值。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!