2012-07-07 112 views
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); 
    } 

} 

它運作良好,當它進入一個字符串,刪除字符串的空格,但落後的顯示字符串時,對話框返回菜單。請幫我看看代碼中出了什麼問題。非常感謝你!

+0

哪個對話框? – 2012-07-07 15:55:35

+0

你好@userunknown!我的意思是Java中的InputDialog我很抱歉。 – 2012-07-07 16:07:36

+0

@FirstLady:查看Berry120的答案:案例3沒有InputDialog。順便說一下:這樣的對話應該顯示什麼輸入? – 2012-07-07 16:11:55

回答

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,它將反轉不再有空格的字符串。請享用!

+0

你好隊長!我非常感謝你的幫助!感謝您做出一些修改哈哈我非常感謝! – 2012-07-07 16:19:03

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(); 
+0

哇謝謝你berry120!非常感謝! – 2012-07-07 16:08:08

相關問題