一.方法(函数)
1.定义:
方法也叫代码块,用于实现功能代码的封装,方便调用。主要目的是实现代码复用。
2.方法和函数的关系
方法:(面向对象)在类中定义的成员。属于类创建出的对象的功能。
函数:(面向过程)在类中定义的成员。属于类创建出的对象的功能。
3.定义方法
权限修饰符 其他修饰符 返回值类型 方法名(参数列表){
//方法体
}
调用方法:
通过类调用:类 . 方法名(参数列表);
通过对象调用:对象 . 方法名(参数列表);
注意事项:
1.方法允许有多参和无参
2.方法定义时的参数列表称为形式参数,调用时称为实际参数。
3.方法名称+参数列表构成方法签名。
4.方法名称使用小驼峰命名法,函数的名称一般使用下划线。
5.在方法中可以使用return表示结束当前方法的执行,如果在return之后有值,则表示结束方法并返回结果。
6.如果方法没有返回值,则返回值类型应该设置为void。
4.方法的重载
在同一个类中,如果出现多个方法名称相同,但是参数列表不同,这种方法称为方法重载。
所谓参数列表不同指的是参数个数和参数类型不同。
方法的重载在多态特性中是一种编译时多态。
5.方法递归
特点:
1.自己调用自己
2.递归必须有结束条件
注意:如果方法递归层数过多可能会导致栈溢出错误(StackOverflowError)
6.方法的参数
方法的参数也属于局部变量
如果参数传递的是基础类型,则相当于传的是数据的副本。(值传递)
如果是引用类型,则传递的是引用地址。(引用传递)
练习:
1、在控制台中打印9*9乘法表
public class Demo06 { public static void main(String[] args) { for(int i = 1;i<=9;i++){ for(int j = 1;j<=i;j++){ System.out.print( j+"x"+i+"="+i*j+" "); } System.out.println(" "); } } }
2、定义一个方法,判断输入的数是否为质数,是则返回true,否则返回false。
public class Demo07 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("输入一个数"); int a = scanner.nextInt(); System.out.println(text(a)); } public static boolean text(int x) { int index = 0; for (int a = 1; a <= x; a++) { if (x % a == 0) { index++; } } if (index == 2) { return true; } return false; } }
3、哥德巴赫猜想:任何一个大于等于6的偶数都可以分解为两个质数之和。
即:输入一个大于等于6的数,在控制台打印两个质数。结果如下: 6 = 3 + 3 8 = 3 + 5 16 = 3 + 13 16 = 5 + 11 18 = 5 + 13 18 = 7 + 11
public class Demo08 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (true) { System.out.println("输入一个数"); int a = scanner.nextInt(); if (a < 6||a%2!=0) { System.out.println("输入错误,从新输入"); } else { text1(a); } } } public static boolean text(int x) { int index = 0; for (int a = 1; a <= x; a++) { if (x % a == 0) { index++; } } if (index == 2) { return true; } return false; } public static void text1(int x){ for(int i = 1;i<=x/2;i++){ if(text(i)&&text(x-i)){ System.out.println(x+"="+i+"+"+(x-i)); } } } }
4、判断一个3位整数是否为回文数(正数和倒数值相等),例如:121是回文数,122不是回文数。
public class Demo09 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (true){ System.out.println("输入一个三位数"); int a = scanner.nextInt(); if(a<100){ System.out.println("输入的数字不正确"); }else { System.out.println(text(a)); break; } } } public static boolean text(int x) { int[] i = new int[3]; i[0] = x/100; i[1] = (x/10)%10; i[2] = x%10; if(i[0]==i[2]){ return true; }else { return false; } } }
5、亲密数:如果A的所有因子之和(含1而不含本身)等于B,而且B的所有因子之和 (含1而不含本身)等于A,那么A/B称之为一对亲密数。要求:输出3000以内的所有亲密数。
package cn.hxzy.pro; import java.util.Scanner; public class Demo10 { public static void main(String[] args) { for(int a = 1;a<=3000;a++){ int b = text(a); if((a==text(b))&&(a>b)){ System.out.println(a+" "+b); } } } public static int text(int num){ int index = 0; for(int i = 1;i<num;i++){ if(num%i==0){ index+=i; } } return index; } }
6、读入一个表示年份的整数,判断这一年是否是闰年。如何判断 一个年份是否是闰年:
-
如果这个年份能够被4 整除,且不能被100 整除,则这一年是闰年。例 如,1996 年是闰年,而相应的,1993 年就不是闰年。
如果这个年份能够被100 整除,则这个数必须要能被400 整除,才是闰 年。例如,2000 年是闰年,1900 年不是闰年。
public class Demo11 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("输入年份"); int a = scanner.nextInt(); System.out.println(); if((a%4==0&&a%100!=0)||(a%100==0&&a%400==0)){ System.out.println("闰年"); }else { System.out.println("平年"); } } }
7、读入一个三位数,计算其各位数字之和。例如:123各位数字之和为6
package cn.hxzy.pro; import java.util.Scanner; public class Demo12 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("输入一个数"); int a = scanner.nextInt(); System.out.println(text(a)); } public static int text(int x) { int n = 0; while (x>0){ n = x%10+n; x = x / 10; } return n; } }