借用ArrayList可以很好地解决这一问题,相比于直接在字符串本身做修改更高效。
import java.util.*; public class RouteTest { public static void main(String args[]){ Scanner in = new Scanner(System.in); String inString = in.nextLine(); ArrayList<Character> newString = new ArrayList<>(); for(int i=0; i<inString.length(); i++){ char j = inString.charAt(i); if(j>'a'&&j<'z'){ newString.add(j);} else if(j>='A'&&j<='Z'){ newString.add((char)(j+32)); } } for(char i:newString) {System.out.print(i);} System.out.println(); } }
示例:
输入:
ACdslkdj ,sds , dO
输出:
acdslkdjsdsdo
acdslkdjsdsdo