方法就是完成特定功能的代码块
在很多语言里面都有函数的定义 , 函数在Java中被称为方法

方法格式

修饰符 返回值类型 方法名(参数类型 参数名1,参数类型 参数名2…) {
方法体;
return 返回值;
}

方法格式解释

修饰符 目前记住 public static
返回值类型 用于限定返回值的数据类型
方法名 一个名字,为了方便我们调用方法
参数类型 用于接收调用方法时传入的数据的类型
参数名 用于接收调用方法时传入的数据的变量
方法体 完成功能的代码
return 结束方法,把返回值带给调用者

定义方法注意事项

写一个方法首先有两点需要明确
返回值类型 明确功能结果的数据类型
参数列表 明确有几个参数,以及参数的类型
**//方法的首字母都是小写;变量首字母也是小写
//求和方法的编写
//1.修饰符
//2.返回类型—浮点数
//3.方法名 有意义 能看懂
//4.是否有参数,如果有需要几个 参数要考虑类型
//5.是否要写return—如果有返回类型,必须要写return
//6.根据要求写方法体

//调试
//1.设置断点
//2.debug启动
//3.step over 执行这条代码(如果这条代码中调用其他方法,不会进入这个调用的方法中)
//4.strp into 执行这条代码(如果这条代码中调用其他方法,会进入这个调用的方法中)
//5.resume progrome 执行完剩下的代码

  1. /**
  2. * @author Lynn
  3. * @create 2020-11-19-15:21
  4. */
  5. public class Method02 {
  6. //方法的首字母都是小写;变量首字母也是小写
  7. //求和方法的编写
  8. //1.修饰符
  9. //2.返回类型--浮点数
  10. //3.方法名 有意义 能看懂
  11. //4.是否有参数,如果有需要几个 参数要考虑类型
  12. //5.是否要写return--如果有返回类型,必须要写return
  13. //6.根据要求写方法体
  14. public static float add(float f1,float f2){
  15. float f=f1+f2;
  16. return f;
  17. }
  18. //调试
  19. //1.设置断点
  20. //2.debug启动
  21. //3.step over 执行这条代码(如果这条代码中调用其他方法,不会进入这个调用的方法中)
  22. //4.strp into 执行这条代码(如果这条代码中调用其他方法,会进入这个调用的方法中)
  23. //5.resume progrome 执行完剩下的代码
  24. public static void main(String[] args) {
  25. //调用方法 鼠标放在调用的方法上,按住Ctrl键可以查看方法返回类型
  26. float ff=add(3.14f,8.88f);
  27. System.out.println(ff);
  28. }
  29. }

练习

①有返回值

  1. import java.util.Scanner;
  2. /**
  3. * @author Lynn
  4. * @create 2020-11-19-15:36
  5. */
  6. public class Method03 {
  7. /**
  8. *返回两个数的较大值
  9. */
  10. public static int getMax(int a,int b){
  11. if(a>b) {
  12. return a;
  13. }else{
  14. return b;
  15. }
  16. }
  17. public static void main(String[] args) {
  18. Scanner sc=new Scanner(System.in);
  19. System.out.println("输入第一个数:");
  20. int num1=sc.nextInt();
  21. System.out.println("输入第二个数:");
  22. int num2=sc.nextInt();
  23. //调用方法
  24. int max=getMax(num1,num2);
  25. System.out.println(max);
  26. }
  27. }
  1. import java.util.Scanner;
  2. /**
  3. * @author Lynn
  4. * @create 2020-11-19-15:42
  5. */
  6. /**
  7. * 判断两个数是否相等
  8. */
  9. public class Method04 {
  10. public static boolean equals(int a,int b){
  11. // if(a==b)
  12. // return true;
  13. // else
  14. // return false;
  15. return a==b;
  16. }
  17. public static void main(String[] args) {
  18. Scanner sc=new Scanner(System.in);
  19. System.out.println("第一个数:");
  20. int num1=sc.nextInt();
  21. System.out.println("第一个数:");
  22. int num2=sc.nextInt();
  23. boolean flag=equals(num1,num2);
  24. System.out.println(flag);
  25. }
  26. }
  1. import java.util.Scanner;
  2. /**
  3. * @author Lynn
  4. * @create 2020-11-19-15:46
  5. */
  6. /**
  7. * 返回三个数中的最大值
  8. */
  9. public class Method05 {
  10. public static int getMaxForThree(int a,int b,int c){
  11. if(a>b){
  12. if(a>c)
  13. return a;
  14. else
  15. return c;
  16. }else{
  17. if(b>c)
  18. return b;
  19. else
  20. return c;
  21. }
  22. }
  23. public static void main(String[] args) {
  24. Scanner sc=new Scanner(System.in);
  25. System.out.println("第一个数:");
  26. int num1=sc.nextInt();
  27. System.out.println("第二个数:");
  28. int num2=sc.nextInt();
  29. System.out.println("第三个数:");
  30. int num3=sc.nextInt();
  31. int max=getMaxForThree(num1,num2,num3);
  32. System.out.println(max);
  33. }
  34. }

②没有返回值

  1. import java.util.PrimitiveIterator;
  2. /**
  3. * @author Lynn
  4. * @create 2020-11-19-15:51
  5. */
  6. public class Method06 {
  7. public static void print(){
  8. for (int i = 0; i < 10; i++) {
  9. System.out.println("Hello World");
  10. }
  11. }
  12. public static void main(String[] args) {
  13. print();
  14. }
  15. }
  1. import java.util.Scanner;
  2. /**
  3. * @author Lynn
  4. * @create 2020-11-19-15:54
  5. */
  6. public class Method07 {
  7. /**
  8. * 输入一个数,输出1-这个数之间的数
  9. * @param i
  10. */
  11. public static void print(int i){
  12. if(i<=1){
  13. System.out.println("数据大于1");
  14. //注意!!!
  15. return;//结束这个方法,不是返回这个值
  16. }
  17. for (int j = 1; j <=i ; j++) {
  18. System.out.print(j+"\t");
  19. }
  20. }
  21. public static void main(String[] args) {
  22. Scanner sc=new Scanner(System.in);
  23. System.out.println("输入一个数:");
  24. int num=sc.nextInt();
  25. print(num);
  26. }
  27. }
  1. /**
  2. * @author Lynn
  3. * @create 2020-11-19-15:56
  4. */
  5. /**
  6. * 打印所有的水仙花数
  7. */
  8. public class Method08 {
  9. public static void printShuiXianHua(){
  10. for (int i = 100; i <=999; i++) {
  11. int ge=i%10;
  12. int shi=i/10%10;
  13. int bai=i/100;
  14. if(ge*ge*ge+shi*shi*shi+bai*bai*bai==i){
  15. System.out.println(i);
  16. }
  17. }
  18. }
  19. public static void main(String[] args) {
  20. printShuiXianHua();
  21. }
  22. }