一个程序互交式逆向输出文字的JAVA算法

mac2022-06-30  56

这是一个程序互交式的读取用户输入的多行文字,然后逆向地打印出这些文字,如果用户输入“tiuq”,程序则退出。

 

import java.io.*;

public class Test4 {    public static void main(String args[]) throws IOException {        char tmp;        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));        for (; ; ) {            System.out.print(">");            String line = in.readLine();

            StringBuffer buf = new StringBuffer(line);

            for (int i = 0; i < buf.length() / 2; i++)

            {                char c = buf.charAt(i);                char d = buf.charAt(buf.length() - 1 - i);                tmp = c;                buf.setCharAt(i, d);                buf.setCharAt(buf.length() - 1 - i, tmp);            }            if (buf.toString().equals("quit")) {                break;            }            System.out.println(buf);

        }    }

转载于:https://www.cnblogs.com/J2EEPLUS/archive/2007/11/28/2487839.html

相关资源:垃圾分类数据集及代码
最新回复(0)