【问题标题】:BlueJ Java compound conditionBlueJ Java 复合条件
【发布时间】:2014-01-06 00:42:58
【问题描述】:

我目前正在编写一些代码并且遇到问题并且无法理解它。我对此很陌生,如果有任何帮助,将不胜感激

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

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
        }

}   

【问题讨论】:

  • @user3113362:请详细说明“复合条件”的含义。此外,如果您可以显示您想要获得的结果,这将有所帮助。您是否只是想了解如何创建条件的逻辑组合(在您的情况下,您可能正在寻找 OR,“||”而不是“|”)。如果是这样,那么请以这种方式提出问题,并避免在您的问题中添加不必要的代码。当你只问问题的本质时,你更有可能得到一个好的答案。

标签: java conditional bluej compound-assignment


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多