0

下面的程序应该这样做:

当用户输入时: The quick brown fox. Jumps over. The lazy dog.
它应该输出:God yzal eth. Revo spmuj. Xof nworb kciuq eht.

但是当用户输入时:The quick brown fox. Jumps over. The lazy dog.
它输出:xof nworb kciuq ehT. revo spmuJ . god yzal ehT .

所以这是我的代码:

import java.util.*;
public class Reverse {
public static void main( String args[] ) {
    String paragraph;
    Scanner input = new Scanner (System.in);

    System.out.print("Enter a paragraph: ");
    paragraph = input.nextLine();
    paragraph = paragraph.trim();
    StringTokenizer tokens = new StringTokenizer(paragraph, ".");
    while (tokens.hasMoreTokens()){
        String ss = tokens.nextToken();
        for (int i = ss.length() - 1; i>=0; i--) {
            System.out.print(ss.charAt(i));
        }
        System.out.print(". ");
    }
    System.out.println();
}
}

我也想到了这个:

import java.util.*;

public class Reverse{

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

    System.out.print("Enter a paragraph: ");
    paragraph = input.nextLine();
    paragraph = paragraph.trim();

    StringTokenizer parWordTokenizer = null;
    StringTokenizer parDotTokenizer = null;
    int numParDotTokens = 0;
    int numParWordTokens = 0;

    parDotTokenizer = new StringTokenizer (paragraph, ".");
    parWordTokenizer = new StringTokenizer (paragraph);
    numParDotTokens = parDotTokenizer.countTokens();
    numParWordTokens = parWordTokenizer.countTokens();

    String[] sentences = new String[numParDotTokens];
    String[] words = new String[numParWordTokens];
    String[] characters = new String[5];

    for (int i=0; i<numParDotTokens; i++) {
        sentences[i] = parDotTokenizer.nextToken();
        System.out.print("S: "+sentences[i]+"\n");
        parWordTokenizer = new StringTokenizer (sentences[i]);
        numParWordTokens = parWordTokenizer.countTokens();
        while (parDotTokenizer.hasMoreTokens()){
            String ss = parDotTokenizer.nextToken();
            for (int k = ss.length() - 1; k>=0; k--) {
                System.out.print(ss.charAt(k));
            }
            System.out.print(". ");
        }
        for (int j=0; j<numParWordTokens; j++) {
            words[j] = parWordTokenizer.nextToken();
            System.out.print("W: "+words[j]+"\n");
            while (parWordTokenizer.hasMoreTokens()){
                String ss = parWordTokenizer.nextToken();
                for (int k = ss.length() - 1; k>=0; k--) {
                    System.out.print(ss.charAt(k));
                }
                System.out.print(". ");
            }
        }
        }
    } } 

revo spmuJ . god yzal ehT . kciuq. nworb. xof. Exception in thread "main" java.util.NoSuchElementException at java.util.StringTokenizer.nextToken(Unknown Source) at ScrambleParagraph.main(Reverse.java:40) 请帮我解决它。几天来我一直在考虑正确的实现,但我是 Java 新手,所以至少现在我做不到。非常感谢你!

4

3 回答 3

1

这大致是我的做法:

public static void main(String[] args) {

    String paragraph;
    Scanner input = new Scanner(System.in);

    System.out.print("Enter a paragraph: ");
    paragraph = input.nextLine();
    paragraph = paragraph.trim();

    String reversedParagraph = new StringBuilder(paragraph)
            .reverse().toString().toLowerCase();

    StringBuilder out = new StringBuilder();
    for (String sentence : reversedParagraph.split(" *\\.")) {
        if (!sentence.equals("")) {
            sentence += ". ";
            out.append(Character.toUpperCase(sentence.charAt(0))
                    + sentence.substring(1));
        }
    }
    System.out.println(out.toString().trim());

}

给定输入:"The quick brown fox. Jumps over. The lazy dog."

这会产生:"God yzal eht. Revo spmuj. Xof nworb kciuq eht."

这似乎是你所要求的。

特殊成分是:StringBuilder.reverse()将字符串反转并 String.split(regex)根据正则表达式将字符串拆分为多个片段。在我颠倒它之后,我用它来遍历段落中的每个句子。

于 2012-07-22T08:45:41.103 回答
1

这样会简单得多。

public String inverse(String str) {
    StringBuffer strBuf= new StringBuffer(str.trim());
    return strBuf.reverse().toString();         
}
于 2014-02-06T18:35:27.777 回答
-2

用“。”分割输入怎么样?首先然后反转每个元素?

public class Reverse {

    public static void main(String[] args) {
        String input = "The quick brown fox. Jumps over. The lazy dog.";
        String[] sentences = input.split("\\.");
        StringBuilder result = new StringBuilder();
        for (int i = sentences.length - 1; i >= 0; i--) {
            String sentence = sentences[i];
            result.append(".").append(
                    new StringBuilder(sentence).reverse().toString());
        }
        System.out.println(result);
    }
}

输出:

.god yzal ehT .revo spmuJ .xof nworb kciuq ehT
于 2012-07-22T08:17:34.070 回答