Skip to content

Commit db82406

Browse files
committed
Chapter 7
1 parent 980eb1f commit db82406

7 files changed

Lines changed: 291 additions & 0 deletions

File tree

ch07/Demo.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
public class Demo
2+
{
3+
public static void main(String[] args)
4+
{
5+
int i = 1;
6+
int sum = 10;
7+
8+
while (i <= 10)
9+
{
10+
System.out.println(i + " " + sum);
11+
i++;
12+
sum = sum + 10;
13+
}
14+
15+
i = 100;
16+
sum = 10;
17+
18+
System.out.println("Starting do while loop!");
19+
20+
do
21+
{
22+
System.out.println(i);
23+
i++;
24+
} while (i <= 10);
25+
26+
System.out.println("Starting for loop");
27+
28+
for (int j = 1; j <= 10; j++)
29+
{
30+
System.out.println(j);
31+
}
32+
}
33+
}

ch07/DoWhile.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public class DoWhile
2+
{
3+
public static void main(String[] args)
4+
{
5+
int num = 100;
6+
7+
do
8+
{
9+
System.out.println("DoWhile Countup: " + num);
10+
num = num + 10;
11+
} while (num < 10);
12+
}
13+
}

ch07/For.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class For
2+
{
3+
public static void main(String[] args)
4+
{
5+
int num = 0;
6+
7+
for (int i = 1; i < 4; i++)
8+
{
9+
System.out.println("Outer Loop i=" + i);
10+
11+
for (int j = 1; j < 4; j++)
12+
{
13+
System.out.print("\tInner Loop= " + j);
14+
System.out.println("\t\tTotal num=" +(++num));
15+
}
16+
}
17+
}
18+
}

ch07/HigherThanOneThousand.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.util.Scanner;
2+
3+
public class HigherThanOneThousand
4+
{
5+
public static void main(String[] args)
6+
{
7+
Scanner in = new Scanner(System.in);
8+
int yourNumber;
9+
10+
System.out.print("Please enter a number: ");
11+
yourNumber = in.nextInt();
12+
13+
int totalCount;
14+
totalCount = getPastOneThousand(yourNumber);
15+
16+
System.out.println("You've entered a total of " + totalCount + " to finally achieve a total greater than 1000!");
17+
18+
}
19+
20+
public static int getPastOneThousand(int numberEntered)
21+
{
22+
Scanner in = new Scanner(System.in);
23+
24+
25+
while (numberEntered < 1000)
26+
{
27+
System.out.print("Enter another number: ");
28+
numberEntered = in.nextInt() + numberEntered;
29+
}
30+
31+
return numberEntered;
32+
}
33+
}

ch07/LoopPractice.java

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
public class LoopPractice
2+
{
3+
public static void main(String[] args)
4+
{
5+
forOneToTen();
6+
whileOneToTen();
7+
doWhileOneToTen();
8+
forTenToOne();
9+
whileTenToOne();
10+
doWhileTenToOne();
11+
forByTens();
12+
whileByTens();
13+
doWhileByTens();
14+
forSequence();
15+
whileSequence();
16+
doWhileSequence();
17+
printOneToNumber(15);
18+
19+
}
20+
21+
public static void forOneToTen()
22+
{
23+
for (int i = 1; i <= 10; i++)
24+
{
25+
System.out.println(i);
26+
}
27+
}
28+
29+
public static void whileOneToTen()
30+
{
31+
int i = 1;
32+
while (i <= 10)
33+
{
34+
System.out.println(i);
35+
i++;
36+
}
37+
}
38+
39+
public static void doWhileOneToTen()
40+
{
41+
int i = 1;
42+
do
43+
{
44+
System.out.println(i);
45+
i++;
46+
} while (i <= 10);
47+
}
48+
49+
public static void forTenToOne()
50+
{
51+
for (int i = 10; i >= 1; i--)
52+
{
53+
System.out.println(i);
54+
}
55+
}
56+
public static void whileTenToOne()
57+
{
58+
int i = 10;
59+
while (i >= 1)
60+
{
61+
System.out.println(i);
62+
i--;
63+
}
64+
}
65+
public static void doWhileTenToOne()
66+
{
67+
int i = 10;
68+
do
69+
{
70+
System.out.println(i);
71+
i--;
72+
} while (i >= 1);
73+
}
74+
75+
public static void forByTens()
76+
{
77+
78+
for (int j = 0; j <= 100; j += 10)
79+
{
80+
System.out.println(j);
81+
}
82+
83+
}
84+
85+
public static void whileByTens()
86+
{
87+
int j = 0;
88+
while (j <= 100)
89+
{
90+
System.out.println(j);
91+
j += 10;
92+
}
93+
}
94+
public static void doWhileByTens()
95+
{
96+
int j = 0;
97+
do
98+
{
99+
System.out.println(j);
100+
j += 10;
101+
} while (j <= 100);
102+
}
103+
104+
public static void forSequence()
105+
{
106+
for (int k = 100; k >= -100; k -= 8)
107+
{
108+
System.out.println(k);
109+
}
110+
}
111+
112+
public static void whileSequence()
113+
{
114+
int k = 100;
115+
while (k >= -100)
116+
{
117+
System.out.println(k);
118+
k -= 8;
119+
}
120+
}
121+
122+
public static void doWhileSequence()
123+
{
124+
int k = 100;
125+
do
126+
{
127+
System.out.println(k);
128+
k -= 8;
129+
} while (k >= -100);
130+
}
131+
132+
public static void printOneToNumber(int n)
133+
{
134+
int k = 1;
135+
while (k <= n)
136+
{
137+
System.out.println(k);
138+
k += 1;
139+
}
140+
}
141+
}

ch07/While.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public class While
2+
{
3+
public static void main(String[] args)
4+
{
5+
int num = 100;
6+
7+
while (num > 0)
8+
{
9+
System.out.println("While Countdown: " + num);
10+
num = num - 10;
11+
}
12+
}
13+
}

ch07/ZeroDestiny.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import org.w3c.dom.css.Counter;
2+
3+
import java.util.Scanner;
4+
5+
public class ZeroDestiny
6+
{
7+
public static void main(String[] args)
8+
{
9+
Scanner input = new Scanner(System.in);
10+
11+
System.out.print("Enter your guess here ---> ");
12+
int yourNumber = input.nextInt();
13+
14+
int count = 0;
15+
count = (youMustEnterZero(yourNumber));
16+
17+
System.out.println("You entered a number other than zero " + count + " number of times.");
18+
19+
}
20+
21+
public static int youMustEnterZero(int n)
22+
{
23+
Scanner input = new Scanner(System.in);
24+
25+
int count = 0;
26+
int k = n;
27+
while (k > 0)
28+
{
29+
System.out.print("Enter another number ---> ");
30+
k = input.nextInt();
31+
count = count + 1;
32+
}
33+
34+
System.out.println("You entered zero!!!!");
35+
36+
return count;
37+
38+
39+
}
40+
}

0 commit comments

Comments
 (0)