设有3个数组内容如下:
String x[]={"zero","one","two", "three",four", "five","six",seven","eight","nine"} ;
String y[]={ten","eleven","twelve","thirteen","fourteen","fifteen", "sixteen","seventeen","eighteen","nineteen" };
String z[]={"twenty","thirty","fourty","fifty", "sixty","seventy", "eighty","ninety" };
用Java命令行输入一个小于100的整数,将其翻成英文表示。
例如:
输入32,输出:thirty two
输入14,输出:fourteen
输入8,输出:eight
反之,输入英文表示,输出相应的整数。
public class test1 {
public static void main(String []args){
System.out.println(CtoE (32));
System.out.println(CtoE (14));
System.out.println(CtoE (8));
}
public static String CtoE(int a){
String x[]={"zero","one","two","three","four", "five","six","seven","eight","nine"} ;
String y[]={"ten","eleven","twelve","thirteen","fourteen","fifteen", "sixteen","seventeen","eighteen","nineteen" };
String z[]={"twenty","thirty","fourty","fifty", "sixty","seventy", "eighty","ninety" };
if(a>=0&a<10){
return x[a];
}
else
if(a>=10&a<20){
return y[a-10];
}
else
if(a>=20&a<100){
if(a%10==0){
return z[a/10];
}else
{return z[a/10-2]+" "+x[a%10] ;}
}
else
{return "this is chaochufanwei";}
}
}
额 我自己做出来了 谁帮我实现英文翻译成数字的把
设有3个数组内容如下: String x[]={"zero","one","two", "three",four&quo
答案:2 悬赏:80 手机版
解决时间 2021-03-06 15:19
- 提问者网友:王者佥
- 2021-03-06 00:29
最佳答案
- 五星知识达人网友:思契十里
- 2021-03-06 01:52
public static void main(String[] args) {
System.out.println(CtoE("twenty two"));
System.out.println(CtoE("ten"));
System.out.println(CtoE("five"));
}
public static String CtoE(String a) {
String shiwei = "";
String gewei = "";
String intShiWei = "";
String intGeWei = "";
a=a.toLowerCase();//全都转换成小写字母
//a.trim()去掉字母前后空格
if(a.trim().indexOf(" ")>-1)
{
shiwei = a.substring(0,a.indexOf(" "));
gewei = a.substring(a.lastIndexOf(" "),a.length());
//十位
if("twenty".equals(shiwei.trim())){
intShiWei ="2";
}else if("thirty".equals(shiwei.trim())){
intShiWei ="3";
}else if("fourty".equals(shiwei.trim())){
intShiWei ="4";
}else if("fifty".equals(shiwei.trim())){
intShiWei ="5";
}else if("sixty".equals(shiwei.trim())){
intShiWei ="6";
}else if("seventy".equals(shiwei.trim())){
intShiWei ="7";
}else if("eighty".equals(shiwei.trim())){
intShiWei ="8";
}else if("ninety".equals(shiwei.trim())){
intShiWei ="9";
}
//个位
if("zero".equals(gewei.trim())){
intGeWei ="0";
}else if("one".equals(gewei.trim())){
intGeWei ="1";
}else if("two".equals(gewei.trim())){
intGeWei ="2";
}else if("three".equals(gewei.trim())){
intGeWei ="3";
}else if("four".equals(gewei.trim())){
intGeWei ="4";
}else if("five".equals(gewei.trim())){
intGeWei ="5";
}else if("six".equals(gewei.trim())){
intGeWei ="6";
}else if("seven".equals(gewei.trim())){
intGeWei ="7";
}else if("eight".equals(gewei.trim())){
intGeWei ="8";
}else if("nine".equals(gewei.trim())){
intGeWei ="9";
}
}else
{
if("zero".equals(a.trim())){
intGeWei ="0";
}else if("one".equals(a.trim())){
intGeWei ="1";
}else if("two".equals(a.trim())){
intGeWei ="2";
}else if("three".equals(a.trim())){
intGeWei ="3";
}else if("four".equals(a.trim())){
intGeWei ="4";
}else if("five".equals(a.trim())){
intGeWei ="5";
}else if("six".equals(a.trim())){
intGeWei ="6";
}else if("seven".equals(a.trim())){
intGeWei ="7";
}else if("eight".equals(a.trim())){
intGeWei ="8";
}else if("nine".equals(a.trim())){
intGeWei ="9";
}else if("ten".equals(a.trim())){
intGeWei ="10";
}else if("eleven".equals(a.trim())){
intGeWei ="11";
}else if("twelve".equals(a.trim())){
intGeWei ="12";
}else if("thirteen".equals(a.trim())){
intGeWei ="13";
}else if("fourteen".equals(a.trim())){
intGeWei ="14";
}else if("fifteen".equals(a.trim())){
intGeWei ="15";
}else if("sixteen".equals(a.trim())){
intGeWei ="16";
}else if("seventeen".equals(a.trim())){
intGeWei ="17";
}else if("eighteen".equals(a.trim())){
intGeWei ="18";
}else if("nineteen".equals(a.trim())){
intGeWei ="19";
}
}
return (intShiWei+intGeWei);
}
这是最笨的方法了,其他的我还不知道,这个方法就是数越大,if越多,可以实现大于100的数,如果你常用的话,可以把这个写成一个通用的工具类
System.out.println(CtoE("twenty two"));
System.out.println(CtoE("ten"));
System.out.println(CtoE("five"));
}
public static String CtoE(String a) {
String shiwei = "";
String gewei = "";
String intShiWei = "";
String intGeWei = "";
a=a.toLowerCase();//全都转换成小写字母
//a.trim()去掉字母前后空格
if(a.trim().indexOf(" ")>-1)
{
shiwei = a.substring(0,a.indexOf(" "));
gewei = a.substring(a.lastIndexOf(" "),a.length());
//十位
if("twenty".equals(shiwei.trim())){
intShiWei ="2";
}else if("thirty".equals(shiwei.trim())){
intShiWei ="3";
}else if("fourty".equals(shiwei.trim())){
intShiWei ="4";
}else if("fifty".equals(shiwei.trim())){
intShiWei ="5";
}else if("sixty".equals(shiwei.trim())){
intShiWei ="6";
}else if("seventy".equals(shiwei.trim())){
intShiWei ="7";
}else if("eighty".equals(shiwei.trim())){
intShiWei ="8";
}else if("ninety".equals(shiwei.trim())){
intShiWei ="9";
}
//个位
if("zero".equals(gewei.trim())){
intGeWei ="0";
}else if("one".equals(gewei.trim())){
intGeWei ="1";
}else if("two".equals(gewei.trim())){
intGeWei ="2";
}else if("three".equals(gewei.trim())){
intGeWei ="3";
}else if("four".equals(gewei.trim())){
intGeWei ="4";
}else if("five".equals(gewei.trim())){
intGeWei ="5";
}else if("six".equals(gewei.trim())){
intGeWei ="6";
}else if("seven".equals(gewei.trim())){
intGeWei ="7";
}else if("eight".equals(gewei.trim())){
intGeWei ="8";
}else if("nine".equals(gewei.trim())){
intGeWei ="9";
}
}else
{
if("zero".equals(a.trim())){
intGeWei ="0";
}else if("one".equals(a.trim())){
intGeWei ="1";
}else if("two".equals(a.trim())){
intGeWei ="2";
}else if("three".equals(a.trim())){
intGeWei ="3";
}else if("four".equals(a.trim())){
intGeWei ="4";
}else if("five".equals(a.trim())){
intGeWei ="5";
}else if("six".equals(a.trim())){
intGeWei ="6";
}else if("seven".equals(a.trim())){
intGeWei ="7";
}else if("eight".equals(a.trim())){
intGeWei ="8";
}else if("nine".equals(a.trim())){
intGeWei ="9";
}else if("ten".equals(a.trim())){
intGeWei ="10";
}else if("eleven".equals(a.trim())){
intGeWei ="11";
}else if("twelve".equals(a.trim())){
intGeWei ="12";
}else if("thirteen".equals(a.trim())){
intGeWei ="13";
}else if("fourteen".equals(a.trim())){
intGeWei ="14";
}else if("fifteen".equals(a.trim())){
intGeWei ="15";
}else if("sixteen".equals(a.trim())){
intGeWei ="16";
}else if("seventeen".equals(a.trim())){
intGeWei ="17";
}else if("eighteen".equals(a.trim())){
intGeWei ="18";
}else if("nineteen".equals(a.trim())){
intGeWei ="19";
}
}
return (intShiWei+intGeWei);
}
这是最笨的方法了,其他的我还不知道,这个方法就是数越大,if越多,可以实现大于100的数,如果你常用的话,可以把这个写成一个通用的工具类
全部回答
- 1楼网友:患得患失的劫
- 2021-03-06 02:45
帮你顶下!
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯