Spaces:
Running
Running
Create 15. From one to maximum and back
Browse files
Week 1: Types, condition clauses and loops/15. From one to maximum and back
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
The program initializes the integer variable maximum with a random initial value.
|
| 2 |
+
Your task is to print the numbers from one to the maximum and back from the maximum to one, all underlined. Each number is printed on its own line.
|
| 3 |
+
For example, if the value of the maximum were 4, the program should print the numbers
|
| 4 |
+
|
| 5 |
+
1
|
| 6 |
+
2
|
| 7 |
+
3
|
| 8 |
+
4
|
| 9 |
+
3
|
| 10 |
+
2
|
| 11 |
+
1
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
import java.util.Random;
|
| 17 |
+
|
| 18 |
+
public class Test{
|
| 19 |
+
public static void main(String[] args){
|
| 20 |
+
final Random r = new Random();
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
int maximum = r.nextInt(15) + 5;
|
| 24 |
+
System.out.println("maximum == " + maximum);
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
for (int i=1; i<=maximum; i++) {
|
| 29 |
+
System.out.println(i);
|
| 30 |
+
}
|
| 31 |
+
for (int i=maximum-1; i>0; i--) {
|
| 32 |
+
System.out.println(i);
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
}
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
maximum == 19
|
| 47 |
+
1
|
| 48 |
+
2
|
| 49 |
+
3
|
| 50 |
+
4
|
| 51 |
+
5
|
| 52 |
+
6
|
| 53 |
+
7
|
| 54 |
+
8
|
| 55 |
+
9
|
| 56 |
+
10
|
| 57 |
+
11
|
| 58 |
+
12
|
| 59 |
+
13
|
| 60 |
+
14
|
| 61 |
+
15
|
| 62 |
+
16
|
| 63 |
+
17
|
| 64 |
+
18
|
| 65 |
+
19
|
| 66 |
+
18
|
| 67 |
+
17
|
| 68 |
+
16
|
| 69 |
+
15
|
| 70 |
+
14
|
| 71 |
+
13
|
| 72 |
+
12
|
| 73 |
+
11
|
| 74 |
+
10
|
| 75 |
+
9
|
| 76 |
+
8
|
| 77 |
+
7
|
| 78 |
+
6
|
| 79 |
+
5
|
| 80 |
+
4
|
| 81 |
+
3
|
| 82 |
+
2
|
| 83 |
+
1
|
| 84 |
+
|