奇偶数判断
public class Test2 {
public static void main(String[] args) {
method();
}
public static void method() {
int num = 9;
if (num % 2 == 0) {
System.out.println("偶数");
} else {
System.out.println("奇数");
}
}
}
奇偶数升级
public class Test2 {
public static void main(String[] args) {
isEvenNumber(10);
}
public static void isEvenNumber(int num) {
if (num % 2 == 0) {
System.out.println("偶数");
} else {
System.out.println("奇数");
}
}
}
打印n-m之间的所有奇数
public class Test2 {
public static void main(String[] args) {
print(10, 20);
}
public static void print(int n, int m) {
for (int i = n; i <= m; i++) {
if (i % 2 == 1) {
System.out.println(i);
}
}
}
}
带返回值方法和调用
public class Test2 {
public static void main(String[] args) {
int num = add(10,20);
System.out.println(num);
}
public static int add(int a, int b) {
int c = a +b ;
return c;
}
}
返回值&两数最大值
public class Test2 {
public static void main(String[] args) {
int result = getMax(10,20);
System.out.println(result);
}
public static int getMax(int a,int b){
if (a>b){
return a;
}else{
return b;
}
}
}