方法就是一个代码片段. 类似于 C 语言中的 "函数".
方法存在的意义:
1. 是能够模块化的组织代码(当代码规模比较复杂的时候).
2. 做到代码被重复使用, 一份代码可以在多个位置使用.
3. 让代码更好理解更简单.4. 直接调用现有方法开发, 不必重复造轮子.
代码示例:
1、 编写代码模拟三次密码输入的场景。 最多能输入三次密码,密码正确,提示“登录成功”,密码错误, 可以重新输入,最多输入三次。三次均错,则提示退出程序
import java.util.Scanner; public class Test { public static void login(){ Scanner scanner =new Scanner(System.in); int count = 3; while (count != 0) { System.out.println("请输入你的密码:"); String pass = scanner.nextLine(); if (pass.equals("123456")) { System.out.println("登录成功"); break; }else { count--; System.out.println("你还有"+count+"次机会"); } } if (count == 0){ System.out.println("没机会了"); System.out.println("登录失败"); } }
public static void main(String[] args) { login(); }
}
运行结果:
2、求一个二进制数中1的个数
import java.util.Scanner; public class Test { public static int fus(int n) { int count = 0; for (int i = 1; i <= 32; i++) { if (((n>>>i) & 1) == 1){//哪位有1,就找出来了 count++; } } return count; } public static void main(String[] args) { System.out.println(fus(11));//3 运行结果:
缺点:效率低,不推荐
优化后:
import java.util.Scanner; public class Test { public static int fus(int n) { int count = 0; while (n != 0){ if((n & 1) != 0) { count++; } n = n>>>1; } return count; } public static void main(String[] args) { System.out.println(fus(11)); }
再次优化:
import java.util.Scanner; public class Test { public static int fus(int n) { int count = 0; while (n != 0){ n = n & (n - 1);//每次与操作都会掉一个1 count++; } return count; } public static void main(String[] args) { System.out.println(fus(11)); }
4、获取一个二进制序列中所有的偶数位和奇数位,并输出:
import java.util.Scanner; public class Test { public static void fasUa(int n) { for (int i = 31; i >= 1 ; i--) { System.out.print(((n>>i)&1)+" "); } System.out.println(); for (int j = 30; j >= 0; j--) { System.out.print(((n>>j)&1)+" "); } } public static void main(String[] args) { fasUa(12); }
运行结果:
4、求出0~999999之间的所有“水仙花数”并输出。(“水仙花数”是指一个三位数,其各位数字的立方和确好等于该数本身,如;153=1+5+3?,则153是一个“水仙花数”)
public static void num(int n){ for ( int i = 1 ; i <= n ; i++){ int count = 0; int tmp = i; while (tmp != 0) { count++; tmp = tmp/10; } tmp = i; int sum = 0; while (tmp != 0) { sum = sum +(int) Math.pow(tmp % 10 , count); tmp = tmp / 10; } if (sum == i){ System.out.println(i); } } } public static void main(String[] args) { num(999999); }
运行结果:
5、编写程序数一下 1到 100 的所有整数中出现多少个数字9。 public static int nineOfNum(int n){ int count = 0; for (int i = 1 ; i <= n ; i++ ) { if (i % 10 == 9) { count++; } if (i / 10 == 9) { count++; } } return count; } public static void main(String[] args) { System.out.println(nineOfNum(100)); }
6、 计算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值。
public static double sumOf(int n){ double sum = 0; int f = 1; for (int i = 1 ; i <= n ;i++){ sum += 1.0/i*f; f = -f; } return sum; }
7、n 阶乘的和
public static int factor(int n) { int ret = 1; for (int i = 1; i <= n; i++) { ret *= i; } return ret; } public static int sumNum(int n) { int sum = 0; for (int j = 1; j <= n; j++) { sum += factor(j); } return sum; } public static void main16(String[] args) { System.out.println(factor(5)); }
实参和形参的关系
交换两个整型变量
class Test {
public static void main(String[] args) {
int a = 10;
int b = 20;
swap(a, b);
System.out.println("a = " + a + " b = " + b);
}
public static void swap(int x, int y)
{ int tmp = x; x = y; y = tmp;
}
}// 运行结果a = 10 b = 20
原因分析刚才的代码, 没有完成数据的交换.对于基础类型来说, 形参相当于实参的拷贝. 即 传值调用
使用重载示例
public static int add(int x ,int y) { return x + y; } public static double add(double x,double y,double z) { return x + y + z;
public static void main21(String[] args) { Scanner scanner = new Scanner(System.in); int a = scanner.nextInt(); int b = scanner.nextInt(); int ret = add(a, b); System.out.println(ret); double c = scanner.nextDouble(); double d = scanner.nextDouble(); double e = scanner.nextDouble();
double ret2 = add(c,d,e); System.out.println(ret2); }
重载的规则
针对同一个类:
方法名相同
方法的参数不同(参数个数或者参数类型)
方法的返回值类型不影响重载.