1、方法
为什么要有方法?
开发中出现了大量重复代码,或者重复的功能,循环满足不了需求就需要用到方法:
- 代码臃肿
- 可读性差
- 维护性差
可以用方法改善
1.1 有参数无返回值的方法
语法结构:
public static void 方法名(数据类型 形参1, 数据类型 形参2){
方法体;
}
案例:写一个方法将一个整数逆序换行打印 ----- 有参数无返回值方法
public static void isReversed(int a) { String ss = "a"; String str = a+ss; for(int i = str.length()-2; i>=0; i--) { System.out.println(str.charAt(i)); } }
1.2 有参数有返回值的方法
语法结构:
public static 数据类型 方法名(数据类型 形参1, 数据类型 形参2){
方法体;
}
案例:写一个方法,控制台输入哪一个年份,判断是否是闰年----有参数有返回值方法
public static String isNonleapOrLeap(int year) { return year % 4 == 0 && year % 100 != 0 || year % 400 == 0 ?year+"是平年":year+"是闰年"; }
1.3 无参数无返回值的方法
语法结构:
public static void 方法名(){
方法体;
}
案例:写一个方法判断一个数字是否是正数 -----无返回值无参数方法
public static void isPositive() { Scanner cin = new Scanner(System.in); System.out.print("请输入一个数:"); double d = cin.nextDouble(); String str = d>=0 ? d+"是正数" : d+"是负数"; System.out.println(str); }
1.4 无参数有返回值的方法
语法结构:
public static 数据类型 方法名(){
方法体;
}
案例:写一个方法,判断一个字符是否是英文字符(大写和小写)—有返回值无参数方法
public static String isUpper() { Scanner sin = new Scanner(System.in); char ch = sin.next().charAt(0); return ch >='a' && ch<='z' || ch >= 'A' && ch <= 'Z' ?"是英文字符!!!":"不是英文字符!!!"; }
2、break 和 continue 关键字
注意事项:break 和 continue 都是用在循环中的
break 是终止循环,continue 是跳过本次循环继续下一次循环
3、关于方法的练习题
1.写一个方法,找出两个int类型最大值,并输出打印最大值
public class Demo1 { public static void main(String[] args) { //max(); //max1(89, 67); //System.out.println(max2()); System.out.println(max3(150, 250)); } //求两个int的最大值的 //无参无返回值的方法 public static void max () { int a,b; a = 45; b = 69; if (a >= b) { System.out.println(a); } else { System.out.println(b); } } //有参无返回值的方法 public static void max1(int a, int b) { if (a >= b) { System.out.println(a); } else { System.out.println(b); } } //无参有返回值的方法 public static int max2 () { int a,b; a = 45; b = 32; if (a >= b) { return a; } else { return b; } } //有参有返回值的方法 public static int max3 (int a, int b) { if (a >= b) { return a; } else { return b; } } }
2.控制台输入一个字符,判断这个字符是否是大写的英文字符,如果是打印是大写的,如果不是打印不是大写的英文字符
import java.util.Scanner; public class Demo2 { public static void main(String[] args) { //控制台输入 Scanner scanner = new Scanner(System.in); System.out.println("请输入一个字符:"); char c1 = scanner.next().charAt(0); System.out.println(isUpper(c1)); } //判断一个字符是否是大写的字符 public static String isUpper (char c1) { //if (c1 >= 'A' && c1 <= 'Z') { if (c1 >= 65 && c1 <= 90) { return "是大写的字母"; } else { return "不是大写字母"; } } }
3.判断一个字符是否是数字字符
public class Demo3 { public static void main(String[] args) { isNumber(); } //判断一个字符是否是数字字符 public static void isNumber () { Scanner scanner = new Scanner(System.in); System.out.println("请输入一个字符:"); char c1 = scanner.next().charAt(0); if (c1 >= '0' && c1 <= '9') { System.out.println("是数字字符"); } else { System.out.println("不是数字字符"); } } }
4.逢7过,使用方法的写法
public class Demo3 { public static void main(String[] args) { printNineNineTable(); //printNineNineTable(); printPassSeven(); } public static void printNineNineTable () { for (int i = 1; i <= 9; i++) { for (int j = 1; j <= i; j++) { System.out.print(j + "*" + i + "=" + j * i); } System.out.println(); } } //逢7过 public static void printPassSeven () { for (int i = 1; i < 101; i++) { if (i % 7 == 0 || i % 10 == 7 || i / 10 == 7) { System.out.println("过"); } else { System.out.println(i); } } } }
5.写一个方法,打印出来99乘法表,使用无参无返回值的方法
public class Demo3 { public static void main(String[] args) { printNineNineTable(); printNineNineTable(); } public static void printNineNineTable () { for (int i = 1; i <= 9; i++) { for (int j = 1; j <= i; j++) { System.out.print(j + "*" + i + "=" + j * i); } System.out.println(); } } }