课程: Learning Java 17
Solution: Employee salary
- [Lecturer] Let's create a function in Java that calculates an employee's salary. Your function may look different than mine, and that's okay. There are many solutions to this challenge. To start off, we'll calculate the weekly paycheck. (mouse clicking) (keyboard typing) We can do that by multiplying the amount made per hour and the number of hours worked per week. (keyboard typing) There are 52 weeks in a year so to calculate the yearly salary, we multiply this by 52. (keyboard typing) We'll also add error checking here to make sure hours per week and amount per hour are above zero. (keyboard typing) If they're not above zero, we'll return zero. You can't make a negative salary. To simplify this we can combine our checks with the OR operator. (keyboard typing) The OR operator, or the two parallel lines allow us to combine these Boolean conditions. If hours per week is less than zero or amount per hour is less than zero, we will return zero. Let's run it. The code works as expected. With this challenge, we implemented a function in Java with math and Boolean operators.
随堂练习,边学边练
下载课堂讲义。学练结合,紧跟进度,轻松巩固知识。