It would be nice if the user could say how many terms to sum up.
The user might want to see the sum of the first 10 terms, for example:
1/1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6 + 1/7 + 1/8 + 1/9 + 1/10
Here is the program again, with new blanks for the improvements.
Now the number of terms to sum up
value()
method:
import java.io.* ; class HarmonicSeries { double value( int ___________ ) { int term=1 ; double sum = 0.0; while ( term <= ___________ ) { sum += 1.0/term; // add the next term to sum term++ ; // increment term } return sum; } } class HarmonicTester { public static void main ( String[] args ) throws IOException { BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in) ); HarmonicSeries series = new HarmonicSeries(); System.out.println("How many Terms?"); int limit = Integer.parseInt( stdin.readLine() ); System.out.println("Sum of " + limit + " terms:" + series.value( limit ) ); } }