https://github.com/jobbole/ProgrammingProjectList

  • 逆转字符串——输入一个字符串,将其逆转并输出。

    package com.zrl.github;
    import java.util.Scanner;
    public class ReverseString {
    public static void main(String[] args) {
    Scanner cin = new Scanner(System.in);
    System.out.println("请输入一个字符串:");
    while(cin.hasNext()) {
    StringBuilder sb = new StringBuilder(cin.nextLine()); //定义可变长的线程不安全字符串
    //sb.reverse();
    String sb1 = "";
    int n = sb.length();
    for(int i=0; i<n; i++) {
    char x = sb.charAt(n-i-1);
    sb1 += x;
    }
    System.out.println("输出逆转后的字符串:" + sb1);
    System.out.println("请输入一个字符串:");
    }
    }
    }
05-13 19:48