Sie sind auf Seite 1von 3

EmptyBoard_2.

java

1
2 public class EmptyBoard_2 {
3
4 public static void main(String[] args) {
5 chant(4);
6 chant(7);
7
8 printNumber(4, 9);
9 printNumber(17, 6);
10 printNumber(8,0); // --> Note: as this line will NOT be
executed, (i <= count is FALSE) the method printNumber will not
execute the for loop and goes straight to the System.out.println()
line.
11 printNumber(0,8);
12
13 line(13);
14 line(7);
15 line(35);
16 System.out.println();
17 box(10,3);
18 box(5,4);
19 box(20,7);
20
21 int x = 9;
22 int y = 2;
23 int z = 5;
24
25 mystery(z, y, x); // values would be --> 5, 2, 9
26 mystery(y, x, z); // values would be --> 2, 9, 5
27
28 }
29
30 // Calling a method with single parameters
31 public static void chant (int times) {
32 for (int i = 1; i <= times; i++) {
33 System.out.println("Just a salad...");
34 }
35 System.out.println();
36 }
37
38 // Calling a method with multiple parameters
39 public static void printNumber(int number, int count) {
40 for (int i = 1; i <= count; i++) {
41 System.out.print(number);

Page 1
EmptyBoard_2.java

42 }
43 System.out.println();
44 }
45
46 // Prints a given number of stars plus a line break
47 public static void line(int count) {
48 for (int i = 1; i <= count; i++) {
49 System.out.print("*");
50 }
51 System.out.println();
52 }
53
54 // Prints a box of stars of the given size
55
56 /* Note:
57 * - We are able to call the method "line" from above INSIDE
this new method "box". But the types parameter important!
58 * - If the method "line" had an int parameter and "box" had a
double parameter, we would get an ERROR. Can't convert int to
double.
59 * - Also notice we are able to insert "width" into the "count"
variable position of the method "line" since they are of the same
type int. */
60
61 public static void box(int width, int height) {
62 line(width);
63
64 for (int line = 1; line <= height - 2; line++) {
65 System.out.print("*");
66 for (int space = 1; space <= width - 2; space++) {
67 System.out.print(" ");
68 }
69 System.out.println("*");
70 }
71 line(width);
72 }
73
74
75 // "Parameter Mystery" problem...
76
77 // Note -- ignore what the name of the parameters are (to avoid
confusion). Focus on the data type and order when called in the
main method.
78 public static void mystery(int x, int z, int y) {

Page 2
EmptyBoard_2.java

79 System.out.println(z + " and " + (y - x));


80 }
81
82
83
84
85
86
87
88
89
90 }
91

Page 3

Das könnte Ihnen auch gefallen