2 回答

TA貢獻1878條經(jīng)驗 獲得超4個贊
處理一個字符后獲得兩個條目的原因是您沒有完全閱讀包含該字符的行。
具體來說,您keyboard.nextInt();
在上分支和keyboard.next();
下分支中使用。雖然它們分別讀取下一個整數(shù)和字符,但它們不處理行尾標記。
然后,當您到達循環(huán)的頂部時,您調(diào)用keyboard.nextLine()
which 處理 int (或字符,在刪除所有情況下)之后出現(xiàn)的任何字符,直到行標記結(jié)束。使用預(yù)期的用戶輸入,這只是一個空字符串。
要解決此問題,您需要確保keyboard.nextLine()
在僅讀取整數(shù)或單個字符的情況下通讀。

TA貢獻1877條經(jīng)驗 獲得超1個贊
發(fā)生的事情是,while循環(huán)的條件是
while (!command.equalsIgnoreCase("quit"))
這在英語中的意思是,只要命令不等于“退出”然后運行這個循環(huán)。
在循環(huán)內(nèi)部,命令實際上從未設(shè)置為“退出”。例如,如果我將輸入字符串指定為“abcde”并要求在位置 1 刪除“c”。那么您的邏輯將命令設(shè)置為“刪除”
command = keyboard.nextLine();
然后將最終值打印為“abde”?,F(xiàn)在當循環(huán)結(jié)束時,命令仍然是“刪除”,因此循環(huán)再次執(zhí)行。
一種可能的解決方案是明確詢問用戶是否要使用 do while 循環(huán)重試。也只是一個提示,我看到你使用了 nextInt。建議在下一個 int 之后立即使用 nextLine??吹竭@個的原因:Java Scanner 不等待用戶輸入
如果您想運行更多命令,那么如果您明確征得用戶同意,這就是您的代碼:
public static void main (String[] args) throws java.lang.Exception
{
Scanner keyboard = new Scanner(System.in);
// Output Variables
String userInput = "";
// Variables
String removeChar = "", removeAllChar = "";
int removeIndex = 0;
// First Output
System.out.println("Enter the string to be manipulated");
userInput = keyboard.nextLine();
String command = "";
String retry = "";
// While loop
do {
// Output
System.out.println("Enter your command (reverse, replace first, replace last, remove all, remove)");
command = keyboard.nextLine();
if (command.equalsIgnoreCase("remove")) {
System.out.println("Enter the character to remove");
removeChar = keyboard.nextLine();
int totalCount = 0;
for (int j = 0; j < userInput.length(); j++) {
if (userInput.charAt(j) == removeChar.charAt(0)) {
totalCount = totalCount + 1;
}
}
System.out.println("Enter the " + removeChar
+ " you would like to remove (Not the index - 1 = 1st, 2 = 2nd, etc.):");
removeIndex = keyboard.nextInt();
keyboard.nextLine();
int currentIndex = 1;
if (removeIndex <= totalCount) {
for (int i = 0; i < userInput.length(); i++) {
if (userInput.charAt(i) == removeChar.charAt(0)) {
if (currentIndex == removeIndex) {
String firstpartOfString = userInput.substring(0, i);
String secondpartOfString = userInput.substring(i + 1, userInput.length());
System.out.println("The new sentence is " + firstpartOfString + secondpartOfString);
userInput = firstpartOfString + secondpartOfString;
break;
} else {
currentIndex = currentIndex + 1;
}
}
}
} else {
System.out.println("Can't find " + removeChar + " occuring at " + removeIndex + " int the string.");
}
// Remove All Code
} else if (command.equalsIgnoreCase("remove all")) {
System.out.println("Enter the character to remove");
removeAllChar = keyboard.next();
String newString = "";
for (int i = 0; i < userInput.length(); i++) {
if (userInput.charAt(i) != removeAllChar.charAt(0)) {
newString = newString + userInput.charAt(i);
}
}
userInput = newString;
System.out.println("The new sentence is " + userInput);
}
System.out.println("Do you want to go again?");
retry = keyboard.nextLine();
// Bracket for while loop
}while("yes".equalsIgnoreCase(retry));
}
添加回答
舉報