2014-04-09 84 views
-3
public static void main(String[] args) { 

    String userInput = "";  // stores the user input 
    String vowels = "";  // used to store the vowels contained in the user input 
    String capitalLetters = ""; // used to store the capital letters in the user input 

    int menuChoice;   // stores the menu choice value entered by the user 
    userInput = JOptionPane.showInputDialog("Please type some text in the box below."); 

    do { 
     menuChoice = Integer.parseInt(JOptionPane.showInputDialog("Please choose one option from the menue below: \n\n" 
       + "1. List all upper case letters \n" 
       + "2. List all vowels \n" 
       + "3. Replace vowels with underscore \n" 
       + "4. Show the string in reverse \n" 
       + "5. Show each letter on a separate line\n" 
       + "6. Exit \n\n")); 

     switch (menuChoice) { 
      case 1: // Find all the capital letters in the user input 
       for (int i = 0; i < userInput.length(); i++) { 
        if (Character.isUpperCase(userInput.charAt(i))) { 
         char cap = userInput.charAt(i); 
         capitalLetters += cap + " "; 
        } 
       } 

       JOptionPane.showMessageDialog(null, "The capital letters in the input " + userInput 
         + " are as follows: \n\n" + capitalLetters, 
         "Capital Letters", JOptionPane.INFORMATION_MESSAGE); 

       break; 

      case 2: // Find all the vowels in the user input 
       for (int i = 0; i < userInput.length(); i++) { 
        if ((Character.toUpperCase(userInput.charAt(i)) == 'A') 
          || (Character.toUpperCase(userInput.charAt(i)) == 'E') 
          || (Character.toUpperCase(userInput.charAt(i)) == 'I') 
          || (Character.toUpperCase(userInput.charAt(i)) == 'O') 
          || (Character.toUpperCase(userInput.charAt(i)) == 'U')) { 
         vowels += userInput.charAt(i) + " "; 
        } 
       } 

       JOptionPane.showMessageDialog(null, "The vowels in the input " + userInput 
         + " are as follows: \n\n" + vowels, 
         "Vowels", JOptionPane.INFORMATION_MESSAGE); 

       break; 

      case 3: // Replace vowels with underscores 

       String underscoreVowels = userInput; 

       underscoreVowels = underscoreVowels.replace('A', '_'); 
       underscoreVowels = underscoreVowels.replace('a', '_'); 
       underscoreVowels = underscoreVowels.replace('E', '_'); 
       underscoreVowels = underscoreVowels.replace('e', '_'); 
       underscoreVowels = underscoreVowels.replace('I', '_'); 
       underscoreVowels = underscoreVowels.replace('i', '_'); 
       underscoreVowels = underscoreVowels.replace('O', '_'); 
       underscoreVowels = underscoreVowels.replace('o', '_'); 
       underscoreVowels = underscoreVowels.replace('U', '_'); 
       underscoreVowels = underscoreVowels.replace('u', '_'); 

       JOptionPane.showMessageDialog(null, "The vowels in the input " + userInput 
         + " replaced by underscores looks like this: \n\n" + underscoreVowels, 
         "Vowels", JOptionPane.INFORMATION_MESSAGE); 

       break; 

      case 4: // Show the message entered in reverse 

       String reverseString = ""; 

       for (int i = userInput.length() - 1; i >= 0; i--) { 
        reverseString += userInput.charAt(i); 
       } 
       JOptionPane.showMessageDialog(null, "The input in reverse order is: \n\n" + reverseString, 
         "Reverse Input", JOptionPane.INFORMATION_MESSAGE); 

       break; 

      case 5: // Show the message entered in reverse 

       String multiLineString = ""; 

       for (int i = 0; i <= userInput.length() - 1; i++) { 
        multiLineString += userInput.charAt(i) + "\n"; 
       } 
       JOptionPane.showMessageDialog(null, "Below is each character entered on a separate line: \n\n" + multiLineString, 
         "Reverse Input", JOptionPane.INFORMATION_MESSAGE); 

       break; 

      case 6: // Display a thank you message and exit the program 
       JOptionPane.showMessageDialog(null, "Thanks for using my program!! \n\n Have a great day.", 
         "Exit Program", JOptionPane.INFORMATION_MESSAGE); 
       break; 

      default: // Display and error message if they entered a value not found on the menu 
       JOptionPane.showMessageDialog(null, "*** ERROR *** \n\nThe value you entered, is not a menu option. \n\n Plesee try again.", 
         "ERROR", JOptionPane.ERROR_MESSAGE); 
       break; 
     }// end switch 

    }// end do 
    while (menuChoice != 6); 
    System.exit(0); 
} 

好的我只是需要一些指導。但對於上述每種情況,都需要有自己的方法,並且我需要基本上編輯該程序,方法是將每個switch-case語句中的功能移到新方法中。新方法應處理所有字符串操作,然後將編輯後的字符串傳回給主方法以完成向用戶輸出。提前致謝!字符串方法幫助Java

+0

我明白你想要做什麼。我不明白問題在哪裏,你的嘗試在哪裏? – Kon

+0

作爲一個開始,我會建議編輯你的程序,將這些案例陳述中的功能轉移到他們自己的新方法中。這些方法可以處理所有的字符串操作,並且可以將編輯後的字符串返回給主方法。輸出到用戶可以在main中完成。 –

+0

因爲疾病,我錯過了幾天..所以我有點落後,你可以讓我看起來像例1像例1,所以我可以找出其餘的...和@Kon我錯過了幾天之前這樣我沒有理念從哪裏開始。 – user3348515

回答

2

我要去當你在評論中提到的證明的情況下1

設置你在正確的方向,這種情況下越過某些字符串中的字符,並把所有的資本人物在新由空格字符分隔的字符串" "。例如,Hello WorlD返回字符串H W D

所以讓我們把所有這些代碼放在一個方法中。應該是直截了當的。

private static String caseOneMethod(String input) { 
    //Code here 
} 

這很簡單。這創建了一個新的方法,它接受一個字符串輸入併產生一個字符串輸出,這正是你想要的。現在你可以用你的Case 1代替:

case 1: // Find all the capital letters in the user input 
    capitalLetters = caseOneMethod(userInput); 

    JOptionPane.showMessageDialog(null, "The capital letters in the input " + userInput + 
    " are as follows: \n\n" + capitalLetters, 
    "Capital Letters", JOptionPane.INFORMATION_MESSAGE); 

    break; 
+0

現在好了,你在那裏的做法,或者在那之外的方法嗎? – user3348515

+0

方法聲明必須在類級別完成。 YOu必須有一些類,很明顯,說'class myHomework {..}',並且在這個類中你可能有幾種方法,包括你在上面發佈的'main()',現在你可以添加'caseOneMethod()' – Kon

+0

如果我這樣做了它應該完成的方式不應該有一個返回聲明或不是? – user3348515