0

再会!我这里有一个 Java 程序,它应该向后显示一个字符串。例如,“番茄酱”应显示“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);
    }

我做了一些更改:您的选项 3 返回到菜单,因为您的 ReverseStr 方法返回了一个反转的字符串,但您从未捕获并显示它。你可能只是赶时间哈哈。我进行了另一项更改,对此我可能错了,但是当您想反转字符串时,它不会将删除空格的字符串发送给它(建议您在选项 3 之前选择选项 2)。我已经这样做了,如果您在选项 3 之前选择选项 2,它将反转不再有空格的字符串。享受!

于 2012-07-07T16:10:38.997 回答