0

良い一日!ここに、文字列を逆方向に表示するJavaプログラムがあります。たとえば、「ketchup」は「puhctek」を表示する必要があります。

import java.util.*;
import java.text.*;
import javax.swing.*;

public class StringManipulation {

    public static String ReverseStr(String S) {
           String newS = "";
           for (int i=0; i<S.length(); i++) {
              newS = S.charAt(i) + newS;
           }
           return newS;
        }
        public static void main(String[] args) {
        int choice;
        String menu, choiceStr = "", enterString="", noSpace;

        do {    
            menu = "MENU \n" +
                    "(1) Enter a string \n" +
                    "(2) Remove all spaces from a string \n" +
                    "(3) Display the string backward \n" +
                    "(4) Quit";
            choiceStr = JOptionPane.showInputDialog(menu);
            choice = Integer.parseInt(choiceStr);
            switch (choice) {
                case 1: enterString = JOptionPane.showInputDialog("Please enter the string:"); 
                        break;
                case 2: noSpace = enterString.replaceAll("\\s", "");
                        JOptionPane.showMessageDialog(null, noSpace); 
                        break;
                case 3: ReverseStr(enterString);
                        break;
                case 4: System.exit(0);
            }
        } while (choice != 4);
    }

}

文字列を入力して文字列のスペースを削除するとうまく機能しますが、文字列を逆方向に表示すると、ダイアログボックスがメニューに戻ります。コードのどこが悪いのか教えてください。どうもありがとうございます!

4

2 に答える 2

3
case 3: ReverseStr(enterString);
    break;

ここで行っているのは、ReverseStrメソッドを呼び出してからブレークアウトすることだけです。結果をユーザーに表示するなど、結果に対して何もしていません。あなたはおそらく次のようなものが欲しいでしょう:

case 3: String rev = ReverseStr(enterString);
    JOptionPane.showMessageDialog(null, rev); 
    break;

ちなみに、Javaで文字列を逆にするための、はるかに簡単で高速な1ライナーは次のとおりです。

new StringBuilder(str).reverse().toString();
于 2012-07-07T15:56:46.723 に答える
0

ここにあなたは男に行きます:

public static void main(String[] args) {
        int choice;
        String menu, choiceStr = "", enterString = "", noSpace;
        String stringWithNoSpaces = "";
        String reversedString = "";

        do {
            menu = "MENU \n"
                    + "(1) Enter a string \n"
                    + "(2) Remove all spaces from a string \n"
                    + "(3) Display the string backward \n"
                    + "(4) Quit";
            choiceStr = JOptionPane.showInputDialog(menu);
            choice = Integer.parseInt(choiceStr);
            switch (choice) {
                case 1:
                    enterString = JOptionPane.showInputDialog("Please enter the string:");
                    stringWithNoSpaces = enterString;
                    break;
                case 2:
                    stringWithNoSpaces = enterString.replaceAll("\\s", "");                    
                    JOptionPane.showMessageDialog(null, stringWithNoSpaces);
                    break;
                case 3:
                    reversedString = ReverseStr(stringWithNoSpaces);
                    JOptionPane.showMessageDialog(null, reversedString);
                    break;
                case 4:
                    System.exit(0);
            }
        } while (choice != 4);
    }

いくつか変更を加えました。ReverseStrメソッドが逆の文字列を返したため、オプション3がメニューに戻りましたが、それをキャッチして表示したことはありません。たぶん急いでいたのでしょう笑。もう1つ変更を加えました。これについては間違っている可能性がありますが、文字列を逆にしたい場合は、スペースが削除された文字列は送信されません(オプション3の前にオプション2を選択したことをお勧めします)。オプション3の前にオプション2を選択すると、スペースがなくなった文字列が反転するように作成しました。楽しみ!

于 2012-07-07T16:10:38.997 に答える