-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathMethodExercise01.java
More file actions
64 lines (54 loc) · 1.18 KB
/
Copy pathMethodExercise01.java
File metadata and controls
64 lines (54 loc) · 1.18 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
public class MethodExercise01 {
//编写一个main方法
public static void main(String[] args) {
AA a = new AA();
// if(a.isOdd(2)) {//T , 这样的写法以后会看到很多
// System.out.println("是奇数");
// } else {
// System.out.println("是偶数");
// }
//
//
// 使用print方法
a.print(4, 4, '#');
}
}
//编写类AA ,有一个方法:判断一个数是奇数odd还是偶数, 返回boolean
class AA {
//思路
//1. 方法的返回类型 boolean
//2. 方法的名字 isOdd
//3. 方法的形参 (int num)
//4. 方法体 , 判断
public boolean isOdd(int num) {
// if(num % 2 != 0) {
// return true;
// } else {
// return false;
// }
//return num % 2 != 0 ? true; false;
//
return num % 2 != 0;
}
//根据行、列、字符打印 对应行数和列数的字符,
//比如:行:4,列:4,字符#,则打印相应的效果
/*
####
####
####
####
*/
//思路
//1. 方法的返回类型 void
//2. 方法的名字 print
//3. 方法的形参 (int row, int col, char c)
//4. 方法体 , 循环
public void print(int row, int col, char c) {
for(int i = 0; i < row; i++) {
for(int j = 0; j < col; j++) {//输出每一行
System.out.print(c);
}
System.out.println(); //换行
}
}
}