BlueJ Java复合条件

时间:2013-12-18 01:15:46

标签: java conditional bluej compound-assignment

目前我正在做一些代码并遇到问题,无法理解它。我很新,这是任何帮助将不胜感激

我正在尝试为日期制定复合条件,但似乎无法理解如何做到这一点。

public void rentBook(int theBorrowerNumber, String theReturnDate)
{

        if(theBorrowerNumber <=0)                                         // checks if the borrower number is less or equal to 0
        {
            System.out.println("Invalid borrower number");                // prints the error message
        }
    ///////////////////////////////////////
    returnDate = theReturnDate;                                           // reDate aquires theReDate content
    String day = returnDate.substring(0,2);                               // set the day to the first 2 numbers from the date
    String month = returnDate.substring(2,4);                             // set the month to the second 2 numbers form the date
    int theDay = Integer.parseInt(day);                                   // converts the day string to integer
    int theMonth = Integer.parseInt(month);                               // converts the month string to integer
    //////////////////////////////////////  
        if(theDay <= 0 | theDay >= 32 | theMonth <= 0 | theMonth >= 13)   //setting the parameters the date must be within
        {
            System.out.println("Invalid date");                           // prints out if the date is wrong
        }
        else
        {
            super.setBorrowerNumber(theBorrowerNumber);                  // sets the borrower number in the super class
        }

}   

1 个答案:

答案 0 :(得分:1)

逻辑或运算符为||而不是|

if(theDay <= 0 || theDay >= 32 || theMonth <= 0 || theMonth >= 13)