ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Day09

2021-05-05 22:57:50  阅读:158  来源: 互联网

标签:Day09 nmuber2 nmuber1 int System result public


流程控制练习

打印三角形

public class shiyan {
    public static void main(String[] args) {
//打印三角形   5行
        for (int i = 1; i <= 5; i++) {
            for (int j = 5; j >= i; j--) {
                System.out.print(" ");
            }
            for (int a = 1; a <= i; a++) {
                System.out.print("*");
            }
            for (int b = 1; b < i; b++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

Debug的使用方法

在想了解的地方打上小断点,点击蟑螂开始运行,该程序会在断点处停下,点击蓝色箭头,该程序会往下再执行一步。

java方法详解

何为方法

System(类).out(输出对象).println()(方法);

方法是语句的集合,它们在一起执行一个功能。

public class shiyan {
    //main方法
    public static void main(String[] args) {
        int sum = add(1, 2);
        //输入add方法时直接输入add(变量值1,变量值2)就可以了
        System.out.println(sum);

    }
//加法
    public static int add(int a,int b){
        return a+b;
    }
}

设计方法的原则:一个方法只完成一个功能

加入static,用于方法的调用,void表示空,不返回值

public class shiyan {
    //main方法
    public static void main(String[] args) {
test();
//以test命名了打印三角形的方法,在psvm可直接调用该方法
    }
public static void test(){
    for (int i = 1; i <= 5; i++) {
        for (int j = 5; j >= i; j--) {
            System.out.print(" ");
        }
        for (int a = 1; a <= i; a++) {
            System.out.print("*");
        }
        for (int b = 1; b < i; b++) {
            System.out.print("*");
        }
        System.out.println();
}
}}

方法的定义

java的方法类似于其他语言的函数,用来完成一些特定功能的代码片段。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5f0sKxmB-1620225913262)(C:\Users\86177\Desktop\捕获1.PNG)]

public class shiyan {
    public static void main(String[] args) {
        //实际参数,用来实际调用传递给他的参数
        int sum = add(1, 2);
        System.out.println(sum);

    }
    //形式参数,用来定义作用的
    public static int add(int a,int b){
        return a+b;
    }
}

方法的调用

调用方法:对象名。方法名(实参列表)

当方法返回一个值的时候,方法调用通常被当做一个值

如: int max=max(10,20);

如果方法返回值是void,方法调用一定是一条语句。

System.out.println("nmuber1==nmuber2");
public class shiyan {
    public static void main(String[] args) {
int max=max(10,20);
        System.out.println(max);
    }
    public  static  int max(int nmuber1, int nmuber2) {
        int result = 0;
        if (nmuber1==nmuber2){
            System.out.println("nmuber1==nmuber2");

            return 0;//终止方法
        }
        if (nmuber1>nmuber2){
            result = nmuber1;
        }else {
            result = nmuber2;
        }return result;
    }
    }

方法的重载

重载就是在一个类中,有相同的函数名称,但形参不同的参数。

public class shiyan {
    public static void main(String[] args) {
        int max=max(10,20);
        System.out.println(max);
    }
    public  static  double max(double nmuber1,double nmuber2) {
       double result = 0;
        if (nmuber1==nmuber2){
            System.out.println("nmuber1==nmuber2");

            return 0;//终止方法
        }
        if (nmuber1>nmuber2){
            result = nmuber1;
        }else {
            result = nmuber2;
        }return result;
    }
    public  static  int max(int nmuber1,int nmuber2) {
        int result = 0;
        if (nmuber1==nmuber2){
            System.out.println("nmuber1==nmuber2");

            return 0;//终止方法
        }
        if (nmuber1>nmuber2){
            result = nmuber1;
        }else {
            result = nmuber2;
        }return result;
    }
}

通过参数的不同,系统会自动辨认调用哪个方法,即使两名字相同

方法可以有无限多重名的,但需确保类型不同。反正就是保证括号里的不一样就行了

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JRlumTgo-1620226162990)(C:\Users\86177\Desktop\捕获1.PNG)]

public class shiyan {   
 public static void main(String[] args) {     
    int max=max(10,20);        System.out.println(max);    }  
      public  static  double max(double nmuber1,double nmuber2) {
      //用double表示       
      double result = 0;        
      if (nmuber1==nmuber2){System.out.println("nmuber1==nmuber2");          
        return 0;//终止方法     
           }       
            if (nmuber1>nmuber2){         
               result = nmuber1;      
        }else {            result = nmuber2;        }
                 return result;    }         
     public  static  int max(int nmuber1,int nmuber2) {
     //用int表示        
     int result = 0;       
      if (nmuber1==nmuber2){ 
      System.out.println("nmuber1==nmuber2");      
            return 0;//终止方法       
         }        if (nmuber1>nmuber2){   
         result = nmuber1;        }else {result = nmuber2;}return result;    }                
             public  static  int max
             (int nmuber1,int nmuber2,int number3) {//用3个int表示        
           int result = 0;        if (nmuber1==nmuber2){
           System.out.println("nmuber1==nmuber2");           
              return 0;//终止方法}       
               if (nmuber1>nmuber2){ 
                result = nmuber1; 
                }else {  result = nmuber2; }
                return result;    }                  
public  static  int max(double nmuber1,double nmuber2,double nmumber3) {//强转       
                   int result = 0;        if (nmuber1==nmuber2){            System.out.println("nmuber1==nmuber2");    
                      return 0;//终止方法        }  
                       if  (nmuber1>nmuber2){ 
        result = (int) nmuber1;  }else {            
          result = (int) nmuber2;        }return result;    }}

命令行传参(没听完)

有时候你希望运行一个程序的时候再传递给它消息,这就要靠传递命令行参数给,main()函数实现

public class shiyan {    public static void main(String[] args) {        //args.length数组长度      for(int i = 0;i <args.length;i++){          System.out.println("args.[" + i + "]"+args[i]);      }    }}

可变参数(建议看完数组重新学习)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-942Gzvg6-1620225913277)(C:\Users\86177\Desktop\捕获3.PNG)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0VC0xJRO-1620225913281)(C:\Users\86177\Desktop\捕获4.PNG)]

public class shiyan {    public static void main(String[] args) {        shiyan shiyan = new shiyan();        shiyan.test(1,2,3);//输入一个1自动补...i    }public void test(int x,int... i){    System.out.println(i[0]);}}

递归

优点,比较方便,可以简易表达数学公式

缺点,基数不能太大,基数太大,运行的程序太多,电脑会直接卡死

所以,尽可能别用递归

public class shiyan {    public static void main(String[] args) {        shiyan test = new shiyan();        test.test();    }public void test(){        test();//错的,会栈溢出}}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-n7DlhGPz-1620225913284)(C:\Users\86177\Desktop\捕获.PNG)]

边界阶段:指的是该递归运行到底,然后开始返回值

前阶段,当参数不满足条件,就一直不停的调用自身

返回阶段:n*(n-1)

public class shiyan {    public static void main(String[] args) {        System.out.println(f(5));}public static int f (int n){        if (n==1){            return 1;        }else {            //2  2*f(1)    f(1)=1            //3  3*f(2)  f(2)=2*f(1)            return n*f(n-1);        }}}

标签:Day09,nmuber2,nmuber1,int,System,result,public
来源: https://blog.csdn.net/qq_56867855/article/details/116431822

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有