Java输入一个字符串,将其中的大写字母转换为小写字母,小写字母转换为大写字母后输出?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java输入一个字符串,将其中的大写字母转换为小写字母,小写字母转换为大写字母后输出?相关的知识,希望对你有一定的参考价值。

java输入一个字符串,将其中的大写字母转换为小写字母,小写字母转换为大写字母后输出。格式是这样

import java.util.Scanner;

public class Main
public static void main(String[] args)

System.out.println("输入一个字符串:");
String str = null;
Scanner cin = new Scanner(System.in);
while (cin.hasNext())
str = cin.nextLine();
break;

String newStr1 = "";
String newStr2 = "";
for (int i = 0; i < str.length(); i++)

if (str.substring(i, i + 1).matches("^[A-Z]+$"))
newStr2 = str.substring(i, i + 1).toLowerCase();
else if (str.substring(i, i + 1).matches("^[a-z]+$"))
newStr2 = str.substring(i, i + 1).toUpperCase();
else
newStr2 = str.substring(i, i + 1);

newStr1 = newStr1 + newStr2;

System.out.println("输出结果:");
System.out.println(newStr1);

参考技术A import java.util.Scanner;

public class Student

public static void main(String[] args)
Scanner sr=new Scanner(System.in);
String st=sr.nextLine();
char[] stc=st.toCharArray();//字符串转单个的字符的数组
for(int i=0;i<stc.length;i++)
char c=stc[i];
if(c>=97&&c<=122)//右边判断字符是不是在编码表小写的范围
c=Character.toUpperCase(c);//变大写
else if(c>=65&&c<=90)//右边判断字符是不是在编码表大写的范围
c=Character.toLowerCase(c);//变小写

System.out.print(c);




参考技术B import java.util.Scanner;
public class test
public static void main(String[] args)
String str,temp="";
str = new Scanner(System.in).nextLine();
for (int i = 0; i < str.length(); i++)
if ((int)str.substring(i,i+1).charAt(0)>=65 && (int)str.substring(i,i+1).charAt(0)<= 90 )
temp+=str.substring(i,i+1).toLowerCase();
else if ((int)str.substring(i,i+1).charAt(0)>=97 && (int)str.substring(i,i+1).charAt(0)<= 122)
temp+=str.substring(i,i+1).toUpperCase();
else
temp+=str.substring(i,i+1);


System.out.println(temp);

以上是关于Java输入一个字符串,将其中的大写字母转换为小写字母,小写字母转换为大写字母后输出?的主要内容,如果未能解决你的问题,请参考以下文章