Why is it that the part of the if statement is an error?

Asked 1 years ago, Updated 1 years ago, 369 views

class Solution {
    public static int vacationRental(int people, int day) {
        // Please complete the function
        if(day<=3) int perDay=80;
        else if (4<=day&day<=9) int perDay=60;
        else int perDay = 50;

        return Math.floor(people*perDay*day*1.12);
        
    }
}

java

2022-09-30 22:04

1 Answers

The variable perDay must be declared only once before it can be used.
Therefore, declare intperDay at the beginning of the class and use it only in each if, else section.

class Solution {
    public static int vacationRental(int people, int day) {
        // Please complete the function
        int perDay;
        if(day<=3)perDay=80;
        else if (4<=day&&day<=9)perDay=60;
        else perDay = 50;

        return(int) Math.floor(people*perDay*day*1.12);
        
    }
}

I think so.


2022-09-30 22:04

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.