再会!我这里有一个 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);
}
}
它在输入字符串时效果很好,删除了字符串的空格,但是当向后显示字符串时,对话框返回菜单。请帮助我代码中有什么问题。非常感谢!