JAVA程序大小写字母的转化
答案:3 悬赏:80
解决时间 2021-03-11 21:26
- 提问者网友:花之森
- 2021-03-11 18:05
代码如下,请问该怎么改呢?注意不能使用系统函数public class change { public static void main(String args[]) { int i; char c; String s="abcDeFG32"; for(i=1;i<=s.length();i++) { c=s.charAt(i-1); if(c>='a'&&c<='z') s.charAt(i-1)=s.charAt(i-1)-32; } }}
最佳答案
- 二级知识专家网友:茫然不知崩溃
- 2021-03-11 18:19
定义一个Map <'a','A'>全26个字母都放进去, 这个为小转大,if()判断为小写,s.charAt(i-1)=map.get(s.charAt(i-q))
全部回答
- 1楼网友:零负荷的放任
- 2021-03-11 19:38
是不能使用toUpperCase()吧。个人提供一个建议,你把你的字符串先转化为字符数组,然后在在字符数组内一个一个的转换。最后再把这个字符数组赋值到原来的字符串中,然后输出字符串。
给你个事例:
String s="asdfDFGH";
char a[];
a=s.toCharArray();
int length=a.length;
for(int i=0;i='a'&&a[i]<='z')
{
a[i]=(char) (a[i]-32);
}
}
s=s.copyValueOf(a);
System.out.println(s);
- 2楼网友:一身浪痞味
- 2021-03-11 19:03
public class optiontest {
public static void main(string args[]) {
string v = joptionpane.showinputdialog(null, "输入一个大写字母:", "test",
joptionpane.question_message);
// int v = integer.parseint(v);
// int offset = (int) 'a' - (int) 'a';// 计算大小写之间的差值
// char lowercase = (char) (v + offset);
string output = "the lowercase is :" + v.tolowercase();
joptionpane.showmessagedialog(null, output, "test",
joptionpane.information_message);
}
}
因为你提示的是输入一个大写字母,所以只简单的处理了一下。
其实使用v.tochararray() 这种方法将其转为char型更好一些。
tochararray()返回一个字符数组。
看看string的api比较好
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯