PDA

View Full Version : Trouble with If..Else statements

jaydub8025
Mar 16th, 2007, 03:00 AM
Hello,

I need to modify my payroll program so that it checks that both the hourly rate and number of hours worked are positive numbers. If either the hourly rate or the number of hours worked is not a positive value, the program should prompt the user to enter a positive amount. I know that this is something that should be quit easy to do, but I am having great trouble in doing this. I know that I have to embed an IF...Else statement here but I keep geting errors when I try to implement the statement. Below is the portion of the program that I have to modify.

// prompt for and input employee's payrate and weekly hours worked
double rate; // first number to multiply
double hours; // second number to mutiply
double product; // product of rate and hours

System.out.print( "Enter employee's payrate: " ); // prompt
rate = input.nextDouble(); // read first number from user

System.out.print( "Enter employee's weekly hours:" ); // prompt
hours = input.nextDouble(); // read second number from user

product = rate * hours; // multiply rate and hours

Mar 16th, 2007, 03:19 AM
Several things...

First, this program isn't javascript. I'm not sure what language it is... is it Java? (Not the same as javascript.)

Second, this looks like a homework assignment. We are not here to do your homework. If you want help, show us what you've tried already and we can give pointers that might help you solve the problem you're having.

Third, this is a good example of a situation that calls for a do...while loop. Here's a pseudocode example. Don't try to compile this; it's just an algorithm:

do {
print "Enter a non-zero number: "
store user input in variable x
if (x == 0) print "Number must not be zero."
} while x == 0
The "do" loop always runs at least once. When it gets to the end, it checks the while condition. If this condition is true, the loop (or more precisely, the code block, enclosed in { } above) repeats. The above example runs until the user enters something other than zero. It should be very easy to turn this to actual code and adapt it for your needs.

Good luck.