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
我明白你想要做什麼。我不明白問題在哪裏,你的嘗試在哪裏? – Kon
作爲一個開始,我會建議編輯你的程序,將這些案例陳述中的功能轉移到他們自己的新方法中。這些方法可以處理所有的字符串操作,並且可以將編輯後的字符串返回給主方法。輸出到用戶可以在main中完成。 –
因爲疾病,我錯過了幾天..所以我有點落後,你可以讓我看起來像例1像例1,所以我可以找出其餘的...和@Kon我錯過了幾天之前這樣我沒有理念從哪裏開始。 – user3348515