1

I want to print the sentence with the characters of each word backwards.

Example: The fox jumped over the fence.

Becomes: ehT xof depmuj revo eht ecnef.

I can only reverse the first word, and then it gave me an ArrayIndexOutOfBoundsException.

4

6 回答 6

4

尝试以下简单代码。这要容易得多:

String[] words = sentence.split(" ");
for (String word : words) {
    System.out.print(new StringBuilder(word).reverse() + " ");
}
于 2013-11-03T04:26:40.597 回答
3

需要对您的代码进行一些更改:

sentence.length()1.改变words.length

2.read.nextLine();使用获取句子作为输入

尝试

import java.util.Scanner;
import java.util.Stack;

public class test {
    public static void main(String[] args) {
        Scanner read = new Scanner(System.in); // Error in line 11
        System.out.print("Enter a sentence: ");
        String sentence = read.nextLine();
        boolean containsPeriod= false;
        if (".".equals(sentence.substring(sentence.length()-1,sentence.length()))){
               sentence = sentence.substring(0,sentence.length()-1);
               containsPeriod=true;
           }


        String[] words = sentence.split(" ");
        for (int i = 0; i < words.length; i++) {
            String word = words[i]; // Error in line 23
            System.out.print(reverseWord(word));
            if(i!=words.length-1)
                System.out.print(" ");
        }
        if(containsPeriod)
            System.out.print(".");
        read.close();
    }

    private static String reverseWord(String sentence) {
        Stack<Character> rev = new Stack();
        for (int ii = 0; ii < sentence.length(); ii++) {
            rev.push(sentence.charAt(ii));
        }
        String out = "";
        while (rev.size() != 0) {
            out += rev.pop();
        }
        return out;
    }
}
于 2013-11-03T04:29:22.790 回答
2

在第一个循环中替换sentence.length()为。words.lengthfor

你想在句子中的每个单词迭代这个循环一次;但是您编写的代码是在句子中的每个字母迭代一次。那是你的错误。

于 2013-11-03T04:26:53.747 回答
1

您可以修复您的代码,如下所示

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Scanner;

public class Query1 {
    public static void main(String[] args) {
        Scanner read = new Scanner(System.in); // Error in line 11

        System.out.print("Enter a sentence: ");
        // String sentence = read.next();
            // read full line here instead of just one word
        String sentence = read.nextLine();
        String[] words = sentence.split(" ");
            // this looks goes to array length
        // for (int i = 0; i < sentence.length(); i++) {
        for (int i = 0; i < words.length; i++) {
            String word = words[i]; // Error in line 23
            // System.out.println(reverseWord(word));
                    // use print not println
            System.out.print(reverseWord(word));
                    // this loop is not at all required
            // for (i = 0; i < words.length - 1; i++) {
            // System.out.println(" ");
            // }
            System.out.print(" ");
        }

    }

    private static String reverseWord(String sentence) {
        Deque<Character> rev = new ArrayDeque<Character>();
        // ArrayStack<Character> rev = new ArrayStack<Character>();

        for (int ii = 0; ii < sentence.length(); ii++) {
            rev.push(sentence.charAt(ii));
        }
        String out = "";
        while (rev.size() != 0) {
            out += rev.pop();

        }

        return out;
    }
}
于 2013-11-03T04:36:42.550 回答
0

尝试这个

public static void main(String args[]) {
        String input = "The fox jumped over the fence";
        StringBuilder sbReverseSentance = new StringBuilder();
        String words[] = input.split(" ");
        for (String word : words) {
            for (int i = word.length() - 1; i >= 0; i--) {
                sbReverseSentance.append(word.charAt(i));
            }
              sbReverseSentance.append(" ");
        }
        System.out.println (sbReverseSentance.toString());
    }
于 2013-11-03T04:30:48.320 回答
0

试试这个,它不处理标点符号。

import java.util.Scanner;

public class Test {

public static void main(String[] args) {
    Scanner read = new Scanner(System.in);

    System.out.print("Enter a sentence: ");
    String sentence = read.nextLine();

    String[] words = sentence.split(" ");

    String[] reversed_words = new String[words.length];

    for (int i = 0; i < words.length; i++) {
        reversed_words[i] = reverseWord(words[i]);
    }

    for (int i = 0; i < reversed_words.length; i++)
        System.out.println(reversed_words[i]);

}

private static String reverseWord(String word) {
    String result = "";
    char[] array = word.toCharArray();
    for (int i = array.length - 1; i >= 0; i--) {
        result += array[i];
    }
    return result;
}
} 
于 2013-11-03T04:33:05.520 回答