老师出的题目。本人初学,啥也不会,请大家帮忙!如:
String a="123.45";
要求:
字符串中的 1 -->a
字符串中的 2 -->b
字符串中的 3 -->c
字符串中的 . -->d
字符串中的 4 -->e
字符串中的 5 -->f
大恩不言谢!
String a="123.45";
要求:
字符串中的 1 -->a
字符串中的 2 -->b
字符串中的 3 -->c
字符串中的 . -->d
字符串中的 4 -->e
字符串中的 5 -->f
大恩不言谢!
调试欢乐多
try{
String str = "1234567890.123";
int num=str.length();
String[] sn= new String[num];String s="";
char[] ch = new char[str.length()];
for(int i = 0; i < str.length(); i++)
{ch[i] = str.charAt(i);
if(ch[i]=='1'){
sn[i]="a";
}else if(ch[i]=='2'){
sn[i]="l";
}else if(ch[i]=='3'){
sn[i]="f";
}else if(ch[i]=='4'){
sn[i]="c";
}else if(ch[i]=='5'){
sn[i]="g";
}else if(ch[i]=='6'){
sn[i]="w";
}else if(ch[i]=='7'){
sn[i]="mh";
}else if(ch[i]=='8'){
sn[i]="d";
}else if(ch[i]=='9'){
sn[i]="e";
}else if(ch[i]=='0'){
sn[i]="n";
}else if(ch[i]=='.'){
sn[i]="s";
}s+=sn[i];
out.println(ch[i]);
}
out.println("<br>"+s);
}catch(Exception e){out.println(e.toString());}
String a="123.45";
要求:
字符串中的 1 -->a
字符串中的 2 -->b
字符串中的 3 -->c
字符串中的 . -->d
字符串中的 4 -->e
字符串中的 5 -->f
因为我不会别的办法呀。所以用这么笨的方式
String str = "1234567890.123";
如何将字符串str中的数字改成对应的字母?
如:1-->a,2-->b……
输出结果为:"abcd……"
基本上就这个样子
String a="123.45";
要求:
字符串中的 1 -->a
字符串中的 2 -->b
字符串中的 3 -->c
字符串中的 . -->d
字符串中的 4 -->e
字符串中的 5 -->fa = a.replaceAll("1","a").replaceAll("2","b").replaceAll("3","c").replaceAll(".","d").replaceAll("4","e").replaceAll("5","f");
String str = "1234567890.123";
char oldChar;
char newChar;
for(int i=0;i<10;i++){
oldChar = i;//进行一下类型转换
newChar = oldChar + ..// asc码转换
str = str.replace(oldChar,newChar);
}
System.out.print(str);
{
public static void main(String args[])
{
int i;
String str[]={"a","b","c","d","e","f"};
String a="123.45";
String s="";
System.out.println("您输入的字符是"+a);
for(i=0;i<a.length();i++)
{ s=s+str[i];}
System.out.println("转换后的字符是"+s);
}
}
要求:
字符串中的 1 -->a
字符串中的 2 -->b
字符串中的 3 -->c
字符串中的 . -->d
字符串中的 4 -->e
字符串中的 5 -->f您好! 原题目是:
String str = "1234567890.123";
如何将字符串str中的数字改成对应的字母?
如:1-->a,2-->b……
输出结果为:"abcd……"
基本上就这个样子=======================
好像没让你换“.”吧。
for(int i=0;i<str.length;i++){
if(str[i]>='0'&&str[i]<'9')
str[i]='a'-'0';
}
这样就把str中的数字转化为字符了
现在的对应规则是 0->a 1->b....
要改的话只要把str[i]='a'-'0'的两个字符替换就可以了
String str = "1234567890.123";
for(int i=0;i<str.length;i++){
if(str[i]>='0'&&str[i]<'9')
str[i]+='a'-'0';
}
这样就把str中的数字转化为字符了
现在的对应规则是 0->a 1->b....
要改的话只要把str[i]+='a'-'0'的两个字符替换就可以了
思路就是上面各位所说的`~呵可~