-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjwelch_module_5.java
More file actions
42 lines (41 loc) · 1.3 KB
/
jwelch_module_5.java
File metadata and controls
42 lines (41 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* jwelch_module_5
*/
public class jwelch_module_5 {
public static void main(String[] args) {
int i = 3;
double result = 0;
String s = "";
//intro:
System.out.println("We will start small and go big, first: ");
System.out.println("The math problem is: ");
while (i <= 99) {
//this is the string for the math problem.
s += "1 / " + i + " + ";
//Using literals to control data types
result = result + 1.0/i;
//increment.
i = i + 2;
}
//chop off last + sign so it can be an equal instead.
s = s.substring(0, s.length() - 2);
System.out.println(s + "= " + result);
System.out.println("This is going down instead of up: ");
//reset variables
i = 99;
s = "";
result = 0.0;
//backwards while loop
while (i >= 3) {
//this is the string for the math problem.
s += "1 / " + i + " + ";
//Using literals to control data types
result = result + 1.0/i;
//increment.
i = i - 2;
}
//chop off last + sign so it can be an equal instead.
s = s.substring(0, s.length() - 2);
System.out.println(s + "= " + result);
}
}