Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Homework help please!!

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello, I am a little stuck on a homework assignment, "Pennies for Pay" basically i have to write a program using a loop that will calculate a persons earnings over a period of time. The person will earn a penny the first day, 2 pennies the second day and continues to double each day and show a table displaying the salary for each day and total pay at the end of the period... My problem is the output i am getting is different than it should be see below:

run:
How many days did you work?
5
Days Worked Amount Paid
--------------------------------------------
1 0.02 <--This should be 1
2 0.04
3 0.08
4 0.16
5 0.32
Your total pay is
$1.62
BUILD SUCCESSFUL (total time: 3 seconds)


Am i doing something wrong with the increment or am i multiplying wrong?? PLEASE HELP!!



 
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I edit because my answer was not correct.

To see why the result in the first case is 0.02 instead of 0.01 you have to check what are you doing in line 45. What value has currentpay in the first loop? and what value are you asigning? maybe and aditional check is neccesary there ;)
 
Marshal
Posts: 69799
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can see what has happened, but you will need additional information to see it yourself.

Insert the following line in your code, after line 44 and before line 45:and this after the present line 47:Note the two lines are very similar

Why are you using DecimalFormat rather than the printf methods? What will happen if somebody enters 0 as number of days worked?

Don't anybody give a complete answer: read this FAQ.
 
Albareto McKenzie
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ey Campbell, you are fast, I was editting my answer

If someone inserts 0 there is a check at line 35
 
Campbell Ritchie
Marshal
Posts: 69799
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Albareto McKenzie wrote:Ey Campbell, you are fast, I was editting my answer

If someone inserts 0 there is a check at line 35

Speed? It's all practice, practice

But don't tell Jessica what the check at line 35 actually does
 
Albareto McKenzie
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
She will find it soon
 
    Bookmark Topic Watch Topic
  • New Topic