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