我有一個問題,我試圖讓一個參數匹配格式'Uddd',其中'U'必須是字母U和'ddd'可以是從0-9的任何3位數字。無法讓java匹配正則表達式匹配()
我當前的代碼:
//borrow method
public boolean borrow(String borrowerID)
{
//if the borrower ID matches the format 'Uddd'
if (borrowerID.matches("U([0-9]{3})"))
{
//if the status is available
if (status == 'A')
{
this.borrowerID = borrowerID;
this.status = 'O';
this.dateBorrowed = currentDate;
return true;
}
//is not available
else
{
return false;
}
}
//does not match format
else
{
return false;
}
}
出於某種原因,它不是驗證正確。當我嘗試輸入'1'作爲參數時,它仍然返回true。
有什麼我失蹤了嗎?
hrmm,我認爲問題在於我的其他子課程。 另外,我注意到你的條件語句不使用任何大括號。這僅適用於單行條件語句嗎? – antonlab 2014-10-19 10:08:13
此外,如果例如在您修改的代碼中,它檢測到狀態不是'A',它會返回false,然後停止代碼? – antonlab 2014-10-19 10:12:42
@antonlab,是的,如果它是單行「塊」,則不需要大括號。此外,「返回」中的內容將跳過其餘的語句。 – paxdiablo 2014-10-19 10:27:48