有人能告訴我一個代碼,用於檢查字符串中特定數量的空格。如何檢查空格的字符串
示例:用戶輸入的字符串需要正好具有3個空格,例如「0 4 0 1」我需要一個代碼來檢查每個數字之間是否有空格。
這裏是我的嘗試吧:
String guessedSpacesCheck = "[0-9]+";
boolean b2 = guessedSpaces.matches(guessedSpacesCheck);
int totalSpace=guessedSpaces.split(" ").length-1;
boolean isSpaceValid = totalSpace==whiteSpace;
while (b2==false && isSpaceValid==false)
{
System.out.println("Your input is not valid. Try again.");
System.out.print("Please enter the letter you want to guess: ");
letterGuess = keyboard.next().charAt(0);
System.out.print("Please enter the spaces you want to check (separated by spaces): ");
guessedSpaces = guessSpace.nextLine();
guessedSpaces.matches(guessedSpacesCheck);
if (guessedSpaces.matches(guessedSpacesCheck)==true && isSpaceValid==true)
{
b2=true;
isSpaceValid=true;
}
}
不錯的要求。請展示一些努力 – Sanjeev
我很抱歉,我試圖嘗試解決這個問題。 – MikeJ