System.out.println("请输入一个字符串:");
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));//定义一个键盘输入流
String line = buf.readLine();//从输入流中读取字符串
byte[]b = line.getBytes();//把字符串转换成数组
for (int x = 0;x<b.length;x++)//通过遍历的方式判断每一位是否字母,是的话,通过+-32来切换大小写
{
int num = b[x];
if (num>=65 && num<=90 || num>=97 && num<=122)
{
b[x] = (byte) ((b[x]>=65 && b[x]<=90)?(b[x]+32): ( b[x]-32));
}
}
line = new String(b);//最后将字节数组转换成字符串
System.out.println(line);
buf.close(); |