用java编写一个程序,实现字符串大小写的转换并倒序输出

如题所述

一下代码仅供参考

public class Main {
public static void main(String[] args) {
String s = "aaBvdEFdsd";

StringBuffer ss = convertString(s);
System.out.println("大小写转换后是:" + convertString(s));
System.out.println("倒序输出后是:" + ss.reverse());
}

public static StringBuffer convertString(String str) {
String upStr = str.toUpperCase();
String lowStr = str.toLowerCase();
StringBuffer buf = new StringBuffer(str.length());
for (int i = 0, k = str.length(); i < k; i++) {
{
if (str.charAt(i) == upStr.charAt(i)) {
buf.append(lowStr.charAt(i));
} else {
buf.append(upStr.charAt(i));
}
}
}
return buf;
}
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2016-03-26
public static void main(String[] args) {
System.out.print("请选择输出的模式【1小写;2大写】:");
Scanner in=new Scanner(System.in);
int choose=in.nextInt();
System.out.print("请输入字符串:");
Scanner inn=new Scanner(System.in);
String str=inn.nextLine();
StringBuffer sb=new StringBuffer(str);
if(choose==1){
System.out.println(sb.reverse().toString().toLowerCase());
}else{
System.out.println(sb.reverse().toString().toUpperCase());
}

}

本回答被提问者采纳
第2个回答  2018-02-28
很简单啊!你先把它变成数组。然后倒着取。在判定这是大写或小写就行,在转换