Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Adding additional $10?

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Location
    Seattle, Washington
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Adding additional $10?

    My program is supposed to calculate the income of the user by taking the number of hours worked and their hourly wage. if the person works over 40 hours, thy recieve 1.5 times the amount of their usual hourly wage. for some reason, when 41 hours is entered with $10 per hour, the program outputs $425 when it should output $415. got any ideas why??
    import java.util.Scanner;
     
    public class WageCalculator {
     
    	public static void main(String[] args) {
    		Scanner scan  = new Scanner(System.in);
    		double hoursworked=0, hoursworked40 = 0, wage, totalpay;
    		int i = 0;
    		System.out.println("Enter number of hours worked: ");
    		hoursworked = scan.nextInt();
    		System.out.println("Enter hourly wage: ");
    		wage = scan.nextInt();
    		while (hoursworked >= 0 && hoursworked <= 168 && i < 1){
    			if(hoursworked > 40){
    				hoursworked40 = hoursworked - 40;
    				hoursworked40 = hoursworked40 * 1.5;
    				totalpay = (hoursworked + hoursworked40) * wage;
    				System.out.println("Total pay: $" + totalpay);
    				i+=1;
    			}else{
    				totalpay = hoursworked * wage;
    				System.out.println("Total pay: $" + totalpay);
    				i+=1;
    			}
    			}
    		if(hoursworked < 0 || hoursworked > 168)
    			System.out.println("Your hours need to be between 0 and 168.");
    		}
    	}
    Thanks in advance


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,618
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Adding additional $10?

    got any ideas why??
    Which line(s) of code is computing the wrong value?
    Add a println() after each line that assigns a value to a variable and print out the variable's value.
    If you know how the computations are supposed to go, you should be able to look at the output and see where the problem is.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    1
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Adding additional $10?

    "totalpay = (hoursworked + hoursworked40) * wage;"
    In that line it calculates the extra hour both in hoursworked(41) and hoursworked40(1.5). You should make "hoursworked=40;" before that line.

  4. The Following User Says Thank You to bad.sector For This Useful Post:

    gotdatdough (November 3rd, 2013)

  5. #4
    Junior Member
    Join Date
    Oct 2013
    Location
    Seattle, Washington
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Adding additional $10?

    Quote Originally Posted by Norm View Post
    Which line(s) of code is computing the wrong value?
    hoursworked40 = hoursworked - 40;
    				hoursworked40 = hoursworked40 * 1.5;
    				totalpay = (hoursworked + hoursworked40) * wage;
    These lines are.

    --- Update ---

    Thank you. i knew it was somehting simple!

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,618
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Adding additional $10?

    These lines are.
    Did you print out the values computed and assigned to see where the problem is?
    What values should each of those expressions compute?
    What values did each of those expressions compute?

    It's nice that someone else did your work and figured it out for you. But you need to learn how to find and fix problems yourself.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Regarding service adding
    By sabarimanoj in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 28th, 2013, 01:16 AM
  2. (Beginner's calculator) Additional calculation after first result...
    By skw4712 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 30th, 2013, 03:56 PM
  3. Adding if statements.
    By LoganC in forum What's Wrong With My Code?
    Replies: 8
    Last Post: October 26th, 2012, 02:11 PM
  4. Java program to do Matrix operation
    By saladfingers73 in forum Collections and Generics
    Replies: 5
    Last Post: March 7th, 2012, 08:17 AM
  5. Help adding non repetition.
    By arkaneraven in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 15th, 2011, 09:31 PM