This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Homework help please!!

 
Jessica Curtner
Greenhorn
Posts: 2
  • 0
  • 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!!



 
Albareto McKenzie
Ranch Hand
Posts: 299
  • 0
  • 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 ;)
 
Campbell Ritchie
Sheriff
Pie
Posts: 47274
52
  • 0
  • 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: 299
  • 0
  • 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
Sheriff
Pie
Posts: 47274
52
  • 0
  • 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: 299
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
She will find it soon
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic