反向单词输入的逻辑校正-[Tom Cat] O / p [Cat Tom],不使用拆分功能

public static void reverse() {

    int x=0;

    char temp = 0;

    String toStrore;

    char checkSpace=' ';

    System.out.println("Enter a line to reverse");

    Scanner sc=new Scanner(System.in);

    String userInput=sc.nextLine();

    char[] charArray=userInput.toCharArray();

    for(int i=charArray.length-1;i>=0;i--){

        char tchar=charArray[i];

        while(tchar==checkSpace){

             x = ++i;

            for(int j=x;j<=charArray.length-1;i++){

                temp=(char) (temp+charArray[j]);

                System.out.print(temp);

            }

        }   

    }

请帮我逻辑。


注意:除lenght()外,我不想使用任何内置功能。


慕虎7371278
浏览 147回答 2
2回答

动漫人物

试试这个import java.util.Arrays;public class Reverse {&nbsp; &nbsp; public static void main(String... args) {&nbsp; &nbsp; &nbsp; &nbsp; getReversed("Tom Cat");&nbsp; &nbsp; }&nbsp; &nbsp; public static void getReversed(String str) {&nbsp; &nbsp; &nbsp; &nbsp; char[] arr = str.toCharArray();&nbsp; &nbsp; &nbsp; &nbsp; int len = 0;&nbsp; &nbsp; &nbsp; &nbsp; for (char ch : arr) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (ch == ' ') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; len++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; String[] res = new String[len + 1];&nbsp; &nbsp; &nbsp; &nbsp; int start = 0;&nbsp; &nbsp; &nbsp; &nbsp; int idx = len;&nbsp; &nbsp; &nbsp; &nbsp; while (idx > -1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StringBuilder builder = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = start; i < arr.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (arr[i] != ' ') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; builder.append(arr[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (i == arr.length - 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res[idx--] = builder.toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res[idx--] = builder.toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; builder.setLength(0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; start = i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(Arrays.toString(res));&nbsp; &nbsp; }}更新了优化代码public static void main(String... args) {&nbsp; &nbsp; String str = "Cat Tom";&nbsp; &nbsp; char[] charArr = str.toCharArray();&nbsp; &nbsp; int len = charArr.length;&nbsp; &nbsp; ArrayList<String> list = new ArrayList<>();&nbsp; &nbsp; StringBuilder stringBuilder = new StringBuilder();&nbsp; &nbsp; for (int i = 0; i < len; i++) {&nbsp; &nbsp; &nbsp; &nbsp; char ch = charArr[i];&nbsp; &nbsp; &nbsp; &nbsp; if (ch == ' ') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.add(stringBuilder.toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stringBuilder = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp; } else if (i == len - 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.add(stringBuilder.append(ch).toString());&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stringBuilder.append(ch);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; Collections.reverse(list);&nbsp; &nbsp; System.out.println(list);}输出[Cat, Tom]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java