简体   繁体   中英

How to set int variable to number-number

I'm pretty new to Java. I'm making a class for a card. The face is the number of the card, and can only be 1-13. I'm trying to validate it by setting f to 1-13. I wanted to simply do this:

public void setFace(int f)
   {

    f int >= 1 && f <= 13);
   }

Obviously, you can't do that. How do would I simply set f to the numbers 1-13? Thanks for any help!

I would do in this way

public void setFace(int f) {

if(f < 1 || f > 13)
{
   throw new IllegalArgumentException("Invalid face value "+f+" face must be between 1-13");
}
}

Angelo

if (f >= 1 && f <= 13) {
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM