Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-26-2011, 02:47 AM   PM User | #1
skylit
New to the CF scene

 
Join Date: Sep 2011
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
skylit is an unknown quantity at this point
Loops problem

I am doing practice problems for class (not homework), and I've gotten most of the coding down except for one stipulation. The exercise reads:

"Write a program that reads input of the number of hours an employee has worked and displays the employee's total and average (per day) number of hours.
The company doesn't pay overtime, so cap any day at 8 hours.

Sample Run:
How many days? 3
Hours? 6
Hours? 12
Hours? 5
Employee's total paid hours = 19
Employee’s average paid hours = 6.3333333"

I can't figure out how to include the bolded stipulation.
I've tried adding:
Code:
 if ( sum >8 ) {
            sum = 8; 
}
but I know that isn't right, and I can't think of any other solution. Can someone advise me on my work?


Code:
    public static void main(String[] args) {
        
    Scanner keyboard = new Scanner(System.in);
   System.out.println("How many days worked? ");
   int count = keyboard.nextInt();
   int i = 1;
   int sum = 0;
    
  while (i <= count) {
      System.out.println("Hours? ");
      sum += keyboard.nextInt();     
      i++;
       
    }
  
  System.out.println("Employee's total paid hours= " +sum);
  System.out.println("Employee's average paid hours= " +(double)sum/count);
  
  
  

}
}
Also! If anyone can explain to me why there has to be an initialization of " int sum = 0 ", that would be great. I just know I have to include it for this loop.

Last edited by skylit; 09-26-2011 at 02:59 AM..
skylit is offline   Reply With Quote
Old 09-26-2011, 03:28 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,542
Thanks: 62
Thanked 4,054 Times in 4,023 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
This is the javaSCRIPT forum.

Your code is written in JAVA.

About the only thing the two languages have in common is the first 4 letters of their names.

*********

Having said that:

In Java (not in JavaScript) *all* variables must be both declared and initialized.

So your variable sum has to be declared and initialized *SOME PLACE* in your code.

And that must be done *OUTSIDE* the loop because inside the loop you are just adding on to what is already there.

But as for the other part of the problem: Easy.

Don't just add the hours worked onto the sum!

*FIRST* assign it to a separate variable. Then use if to find out if it is more than 8 and, if so, change it to 8.

Only *after* doing that do you add that value to sum.

Don't be afraid to use more variables. They don't cost extra, honest.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
skylit (09-26-2011)
Reply

Bookmarks

Tags
loops

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:52 AM.


Advertisement
Log in to turn off these ads.