我一直堅持了一段時間。我需要忽略輸入的數字,空格和特殊字符,如$ ^%## &!和剛讀其他字母AZ,使用Character.isDigit & Character.isLetter ..我用兩種方法試過,但它並沒有爲我工作了..請諮詢..忽略數字,空格和讀取字符串輸入,使用Character.isLetter&Character.isDigit
錯誤:
正常輸出(無空格和數字):
預期的輸出應該是438-5626,甚至當我進入123 $ @ GetLoan。他們應該忽略的前幾個字符「123 $ @ '和只讀GetLoan ..
完整問題:編寫一個程序,提示用戶輸入用字母表示的電話號碼並輸出相應的電話號碼。如果用戶輸入七個以上的字母,則只處理前七個字母。在第三個數字後面輸出 - (連字符)。允許用戶使用大寫和小寫字母以及單詞之間的空格。
public class Question3 {
public static void main(String[] args) {
String letters;
char phoneDigit;
Scanner kb = new Scanner(System.in);
System.out.println("Enter letters : ");
letters = kb.next();
for (int i = 0; i < 7; i++) {
phoneDigit = letters.charAt(i);
if (Character.isLetter(phoneDigit) == true) {
if (i == 3) {
System.out.println("-");
} //If
if (phoneDigit >= 'A' && phoneDigit <= 'C'
|| phoneDigit >= 'a' && phoneDigit <= 'c') {
System.out.println("2");
} else if (phoneDigit >= 'D' && phoneDigit <= 'F'
|| phoneDigit >= 'd' && phoneDigit <= 'f') {
System.out.println("3");
} else if (phoneDigit >= 'G' && phoneDigit <= 'I'
|| phoneDigit >= 'g' && phoneDigit <= 'i') {
System.out.println("4");
} else if (phoneDigit >= 'J' && phoneDigit <= 'L'
|| phoneDigit >= 'j' && phoneDigit <= 'l') {
System.out.println("5");
} else if (phoneDigit >= 'M' && phoneDigit <= 'O'
|| phoneDigit >= 'm' && phoneDigit <= 'o') {
System.out.println("6");
} else if (phoneDigit >= 'P' && phoneDigit <= 'S'
|| phoneDigit >= 'p' && phoneDigit <= 's') {
System.out.println("7");
} else if (phoneDigit >= 'T' && phoneDigit <= 'V'
|| phoneDigit >= 't' && phoneDigit <= 'v') {
System.out.println("8");
} else if (phoneDigit >= 'W' && phoneDigit <= 'Z'
|| phoneDigit >= 'W' && phoneDigit <= 'z') {
System.out.println("9");
} // If
} // If
} // For loop
} //PSVM
請不要使用截圖顯示錯誤消息,在這個問題說明正常輸入。 –
也告訴它如何不工作,什麼是預期的輸出和什麼是輸入。 –