日期:2014-05-20  浏览次数:20907 次

IO流的一编程题 求助--在线等
从键盘输入若干个字符,当输入字符“#”时中止输入。统计输入字符的个数,并将他们按输入时的逆序输出。如:
输入:inputstream#
输出:maertstupni

------解决方案--------------------
Java code
import java.io.*;
import java.util.*;
class hh 
{
    public static void main(String[] args)throws Exception 
    {
        BufferedReader br = 
            new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb = new StringBuffer();
        String line = null;
        while((line=br.readLine())!=null){
            if(line.equals("#"))
                break;
            
            sb.append(line);
            sb.reverse();
            System.out.println(sb);
        }
    //    System.out.println("Hello World!");
    }
}

------解决方案--------------------
Java code

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        int num = 0;
        Scanner input = new Scanner(System.in);
        StringBuffer sb = new StringBuffer();
        while(!input.hasNext("#")){
            sb.append(input.next());
        }
        
        System.out.println(sb.length());
        System.out.println(sb.reverse());
    }
}

------解决方案--------------------
Java code

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        StringBuilder sd = new StringBuilder();
        String s = "";
        while(!(s = in.next()).equals("#"))
        {
            sd.append(s);
        }
        System.out.println(sd.toString().length());
        System.out.println(sd.reverse());
    }
}