java程序对输入的字符进行倒叙输出,并要求在逆序的过程中将所有的空格过滤掉

如题所述

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Test {

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line;
while(!(line = br.readLine()).equalsIgnoreCase("exit")){
System.out.println(filter(line));
}
}

public static String filter(String s){
s = s.replaceAll("\\s", "");
StringBuilder sb = new StringBuilder(s);
sb.reverse();
return sb.toString();
}

}

再给你来个递归版的:
public static void main(String[] args) throws IOException {
filter1();
}

public static void filter1() throws IOException{
int c = System.in.read();
if(c != '\n'){
filter1();
}

if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')){
System.out.print((char)c);
}
}
温馨提示:答案为网友推荐,仅供参考
相似回答