[英]How to clear Scanner in loops?
我是編程新手,這是我編程介紹 class 的第一個學期。 我正在做這個項目,遇到了一些問題。 我只限於使用字符串方法,例如 (length、concat、+、charAt、substring 和 equals(或 equalsIgnoreCase)),並且禁止使用 StringBuilder、indexOf、Arrays。 任何幫助將不勝感激......因為我只是迷路了。
這是我的代碼,它以各種方式操作輸入的字符串。 我已經讓它循環整個過程,直到用戶輸入“退出”。 當連續循環時,結果字符串加倍。 似乎第一個循環的結果與在下一個循環中修改的字符串一起打印。 該字符串不需要清除和重置,但必須由用戶修改直到退出。
好例子:第一個循環:“esrever”(單詞反轉被反轉)和第二個循環:“srvr”(所有字符都被刪除)等等。
我面臨兩個問題:
當 do-while 循環重復時,命令提示符System.out.println("Enter your command (reverse, replace first, replace last, remove all, remove, quit)");
有時打印雙倍。 你能告訴我我在做什么有什么問題嗎?
當 do-while 循環重復時,新結果將連接到之前的結果。 例如,當我反轉我的字符串“reverse”時,我得到“esrever”,當 do while 產生第二個反轉結果時,新反轉的字符串在“esrever”之前連接,導致“esreveresrever”
我用谷歌搜索,發現清除我的掃描儀可以解決,所以我添加了keyboard.nextLine();
像這樣:
do { System.out.println("Enter your command (reverse, replace first, replace last, remove all, remove, quit)"); command = keyboard.nextLine(); keyboard.nextLine();
但這並沒有解決我的問題。 相反,它創建了一個不必要的提示來按 Enter 繼續。 我的方法有什么問題?
試圖解決
do {
System.out.println("Enter your command (reverse, replace first, replace last, remove all, remove, quit)");
command = keyboard.nextLine();
}while(!command.equalsIgnoreCase("quit"));
嘗試通過添加 nextLine 來解決
do {
System.out.println("Enter your command (reverse, replace first, replace last, remove all, remove, quit)");
command = keyboard.nextLine();
keyboard.nextLine();
}while(!command.equalsIgnoreCase("quit"));
這是我的全部代碼
導入 java.util.Scanner;
公共 class StringFun {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String originalString;
String modifiedString="";
String command="";
char enterReplaceChar;
char enterNewChar;
char enterRemoveChar;
int selectedCharToRemove=0;
int whichToRemove;
int stopOrGo=0;
System.out.println("Enter the string to be manupulated");
originalString = keyboard.nextLine();
do {
System.out.println("Enter your command (reverse, replace first, replace last, remove all, remove, quit)");
command = keyboard.nextLine();
//reverse command
if (command.equalsIgnoreCase("reverse")) {
for (int i = originalString.length()-1; i>=0; i--) {
modifiedString = modifiedString + originalString.charAt(i);}
System.out.println("The new sentence is: " + modifiedString);}
//replace first command
else if (command.equalsIgnoreCase("replace first")) {
System.out.println("Enter the character to replace");
enterReplaceChar = keyboard.next().charAt(0);
System.out.println("Enter the new character");
enterNewChar = keyboard.next().charAt(0);
for(int i=0; originalString.length() > i ;i++) {
if (stopOrGo < 1)
if (originalString.charAt(i)== enterReplaceChar) {
stopOrGo++;
modifiedString = modifiedString + enterNewChar;}
else {modifiedString = modifiedString + originalString.charAt(i);}
else {modifiedString = modifiedString + originalString.charAt(i);}}
if (modifiedString.equals(originalString)) {
System.out.println("The letter was not found in the word");
}else {
System.out.println("The new sentence is:"+ modifiedString);}
}
else if (command.equalsIgnoreCase("replace last")) {
System.out.println("Enter the character to replace");
enterReplaceChar = keyboard.next().charAt(0);
System.out.println("Enter the new character");
enterNewChar = keyboard.next().charAt(0);
for(int i=originalString.length()-1; i >= 0 ;i--) {
if (stopOrGo < 1)
if (originalString.charAt(i)== enterReplaceChar) {
stopOrGo++;
modifiedString = enterNewChar + modifiedString;}
else {modifiedString = originalString.charAt(i) + modifiedString;}
else {modifiedString = originalString.charAt(i) + modifiedString;}}
if (modifiedString.equals(originalString)) {
System.out.println("The letter was not found in the word");
}else {
System.out.println("The new sentence is:"+ modifiedString);}
}
//remove all loop
else if (command.equalsIgnoreCase("remove all")) {
System.out.println("Enter the character to remove");
enterRemoveChar = keyboard.next().charAt(0);
for(int i=0; originalString.length() > i ;i++)
if (originalString.charAt(i)== enterRemoveChar) {
modifiedString = modifiedString + originalString.substring(i,i);}
else {modifiedString = modifiedString + originalString.substring(i,i+1);}
if (modifiedString.equals(originalString)) {
System.out.println("Error: the letter you are trying to remove does not exist");
}else {
System.out.println("The new sentence is:"+ modifiedString);}
}
//remove selected loop
else if (command.equalsIgnoreCase("remove")) {
System.out.println("Enter the character to remove");
enterRemoveChar = keyboard.next().charAt(0);
System.out.println("Enter the " + enterRemoveChar + " you would like to remove (Not the index-1=1st, 2=2nd, etc.):");
whichToRemove = keyboard.nextInt();
for(int i=0; originalString.length() > i ;i++) {
if (originalString.charAt(i)==enterRemoveChar) {
selectedCharToRemove++;
if (selectedCharToRemove==whichToRemove) {
modifiedString = modifiedString + originalString.substring(i,i);
}
else {modifiedString = modifiedString + originalString.substring(i,i+1);}}
else {modifiedString = modifiedString + originalString.substring(i,i+1);}}
if (modifiedString.equals(originalString) || (whichToRemove > selectedCharToRemove)) {
System.out.println("Error: the letter you are trying to remove does not exist");
}else {
System.out.println("The new sentence is:"+ modifiedString);}
}
}while(!command.equalsIgnoreCase("quit"));
System.out.println("...Execution Ends...");
keyboard.close();
System.exit(0);
} }
你在這里有幾個問題:
您似乎為此付出了一些努力,因此我將為您的操作提供一些建議(請注意,我使用的是一個名為“modifiableString”的單個字符串變量(而不是 originalString 和 modifiedString),該變量在開始時采用帶有scanner.nextLine() 的程序並用於將來的每個操作)。 隨意嘗試自己重寫它 - 我可以向您保證,有幾種比您迄今為止編寫的循環更簡單的方法。
reverse
modifiableString = new StringBuilder(modifiableString).reverse().toString();
System.out.println("The new string is: " + modifiableString);
replace first
String newModifiableString = modifiableString.replaceFirst(String.valueOf(enterReplaceChar), String.valueOf(enterNewChar));
if (newModifiableString.equals(modifiableString)) {
System.out.println("The letter was not found in the word");
}
else {
modifiableString = newModifiableString;
System.out.println("The new string is: " + modifiableString);
}
replace last
int lastIndexOfReplaceChar = modifiableString.lastIndexOf(enterReplaceChar);
if (lastIndexOfReplaceChar == -1) {
System.out.println("The letter was not found in the word");
}
else {
modifiableString = modifiableString.substring(0, lastIndexOfReplaceChar) + enterNewChar + modifiableString.substring(lastIndexOfReplaceChar + 1);
System.out.println("The new string is: " + modifiableString);
}
remove all
String newModifiableString = modifiableString.replaceAll(String.valueOf(enterRemoveChar), "");
if (newModifiableString.equals(modifiableString)) {
System.out.println("Error: the letter you are trying to remove does not exist");
}
else {
modifiableString = newModifiableString;
System.out.println("The new sentence is: " + modifiableString);
}
remove
這個有點復雜,因為我們需要使用正則表達式匹配器來遍歷字符串,直到我們在正確的索引處找到字符,或者根據字符串中該字符的實例數確定索引是不可能的.
Matcher matcher = Pattern.compile(String.valueOf(enterRemoveChar)).matcher(modifiableString);
if (!matcher.find()) {
System.out.println("Error: the letter you are trying to remove does not exist");
}
else if (whichToRemove < 1 || whichToRemove > modifiableString.length()) {
System.out.println("Error: the index you are trying to remove does not exist");
}
else {
int characterIndex = 1;
while (characterIndex < whichToRemove && matcher.find()) {
characterIndex++;
}
if (characterIndex < whichToRemove) {
System.out.println("Error: the index you are trying to remove does not exist");
}
else {
int offset = matcher.end() - 1;
modifiableString = modifiableString.substring(0, offset) + modifiableString.substring(offset + 1);
System.out.println("The new string is: " + modifiableString);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.