所以我的程序允許用戶輸入一個字符串,然後刪除所有出現的字符。如果該字符不存在於字符串中,那麼它應該打印一條錯誤消息。現在,我創建了一個循環來檢查字符串中的每個字符,以創建沒有字符的新字符串。我不知道如何創建一個輸入驗證循環,而不會爲每個與用戶想要刪除的字符不匹配的字符打印錯誤消息。我希望這是有道理的!輸入驗證循環
這裏是我的代碼的一部分:
//REMOVE LOOP
System.out.println("Enter the character to remove");
String oldChar = keyboard.nextLine();
while (indexEnd <= string.length()) {
String substring = string.substring(indexStart, indexEnd);
indexStart++;
indexEnd++;
}
while (substring.equals(oldChar)) {
substring = string.substring(0, indexStart-1);
string = substring + string.substring(indexEnd - 1);
indexStart=0;
indexend=1;
}
}
我會建議使用其中一種字符串方法(str.replace(c,「」))。也就是說,除非這是一項家庭作業,你必須在循環中完成作業。 –
請添加堆棧跟蹤和代碼。您也可以查看[如何提問](http://stackoverflow.com/help/how-to-ask)來改進問題。歡迎來到SO! –