Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Loops problem

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 09-26-2011, 03: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 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 03:59 AM..
 09-26-2011, 04:28 AM PM User | #2 Old Pedant Supreme Master coder!     Join Date: Feb 2009 Posts: 24,962 Thanks: 75 Thanked 4,309 Times in 4,276 Posts 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.
 Users who have thanked Old Pedant for this post: skylit (09-26-2011)

 Bookmarks

 Tags loops

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 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 Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 11:46 AM.