• March 8th, 2014, 04:11 PM
freefora11
Hello everyone, this should be an easy problem for anyone her I believe because I'm just not noticing why it won't display the answer. I'm trying to solve this book problem......
"Write a for loop that calculates the total of the follower series of numbers:
1/30 + 2/29 + 3/28......+30/1"

Here is what I have..
Quote:

public static void main(String[] args) {
double total = 0;
for (double a = 1, b = 30; b < 1; a++, b--) {
total += (a / b);
}
System.out.println(total);

}
}
When launched, the output is 0.0. I tried changing the variables a and b to doubles but didn't change anything, any help? Thanks!

P.s how do you post a code correctly on this forum?
• March 8th, 2014, 04:26 PM
GregBrannon
Show the version with doubles. Read about 'integer math'.
• March 8th, 2014, 05:34 PM
jashburn
Hi,

To post code in the forum, simply wrap the code with [code] tags, e.g.,

[code]Your
code
here[/code].

The problem is in your 'for' loop:

Code :

`for (double a = 1, b = 30; b < 1; a++, b--) {`

The parts of a 'for' loop:

Code :

`for (initialization; termination; increment)`

When the termination expression evaluates to false, the loop terminates. Your termination expression is b < 1, whereas your initialisation expression for b is b = 30.

If you still don't see the problem, try adding

Code :

```System.out.println("a = " + a); System.out.println("b = " + b);```

to the body of your 'for' loop.
• March 8th, 2014, 05:44 PM
freefora11