java - Sentinel value with while looping -
java - Sentinel value with while looping -
i brand new java , taking first class. i've been struggling homework assignment lastly 2 weeks , need help. assignment write payroll programme prompts pay rate , hours. needs loop continuously until come in sentinel value. 1 time sentinel value entered, average out gross pay me.
i've got continuously loop, asking pay rate , hours. calculates income correctly , loop.
here problem. can't seem sentinel value of -1 work. when seek set end when come in -1, doesn't anything. averaging income after each set of pay rate , hours entered. want wait until end , average income afterwards.
please, input help!
here have far. pulling implementation file test file.
class="lang-java prettyprint-override">import java.util.scanner; public class payroll { private double hours; private double payrate; private double grosspay; public void payroll() { this.hours = hours; this.payrate = payrate; } public void sethours (double hours) { this.hours = hours; } public double gethours () { homecoming hours; } public void setpayrate (double payrate) { this.payrate = payrate; } public double getpayrate () { homecoming payrate; } public double getpay() { homecoming (payrate * hours); } public void calcpay() { scanner input = new scanner(system.in); double total = 0; int paycounter = 0; system.out.printf("enter employee payrate: $"); setpayrate(input.nextdouble()); system.out.printf("enter employee hours worked: "); sethours(input.nextdouble()); double payrate = getpayrate(); while (payrate != -1) //loops until sentinel value read. { if (gethours() > 40) { system.out.printf("total income is: $%.2f", ((getpayrate() * 40)+((gethours() - 40) * (getpayrate() * 1.5)))); grosspay = ((getpayrate() * 40)+((gethours() - 40) * (getpayrate() * 1.5))); paycounter++; } if (gethours() == 40) { system.out.printf("total income is: $%.2f", ((getpayrate() * 40))); grosspay = ((getpayrate() * 40)); paycounter++; } if (gethours() < 40) { system.out.printf("total income $%.2f%n", ((getpayrate() * gethours()))); grosspay = (getpayrate() * gethours()); paycounter++; } if (paycounter!= 0) { total = total + grosspay; //adds grade total double average = (double) total / paycounter; system.out.printf( "%n%nnumber of gross income entered: %d.%n%ntotal $%.2f%n", paycounter, total); system.out.printf("%naverage income is: %.2f%n", average); } else { system.out.println("no pay rate entered."); } system.out.printf("%nenter employee payrate: $"); setpayrate(input.nextdouble()); system.out.printf("%nenter employee hours worked: %n"); sethours(input.nextdouble()); } } }
you set double payrate 1 time in function calcpay, loop on status not alter (because don't set again).
java loops if-statement while-loop sentinel
Comments
Post a Comment