简体   繁体   中英

Java scanner and count not working

I want the code to 'count' the number of hours entered however it is not working. Is the 'hours++' correct? Can someone please help me. Thanks.

    Service temp = null;

    if (temp == null) {
        System.out.printf("Error!");
    } else {
        System.out.printf("Enter hours worked: ");
        hoursWorked = sc.nextDouble();
        boolean recordHours = temp.recordHours(hoursWorked);
        sc.nextLine();

        if (recordHours == true) {
            System.out.printf("Worked hours recorded for " + "\"" + a + "\"");
        }
        else
            System.out.printf("Error!");
        System.out.println();
    }
}


public boolean recordHours(double hours) {
    if (hours <= 0) 
        return false;
    else {
        hours++;
        return true;
    }
}

Java is a pass by value language. Changing hours inside recordHours only changes a local variable, and not the original variable passed to the method.

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