首页 > 代码库 > Linux运维系统工程师与java基础学习系列-8

Linux运维系统工程师与java基础学习系列-8

Java天生骄傲系列-8

  函数的应用(重点掌握

如何定义函数

         1

                             package test.myeclipse;

                publicclass test1 {

                                  publicstaticvoid main(String[]args) {

                                      int Sum = getSum();

                                      System.out.println("Sum="+Sum);

          

                                   }      

      

                                  publicstaticint getSum()

                                  {

                                      return 3+4;

                                  }

 

                }

            运行结果:Sum=7

 

        例2

                package test.myeclipse;

                publicclass test1 {

                                  publicstaticvoid main(String[]args) {

                                     int Sum = getSum(5,7);

                                     System.out.println("Sum="+Sum);

                                   }     

      

                                  publicstaticint getSum(int x,int y)

                                  {

                                      return x+y;

                                  }

 

                }

 

            运行结果:Sum=12

 

 

        牛刀小试:

                          packagetest.myeclipse;

            publicclass test1 { 

                              publicstaticvoid main(String[]args) {

                                  draw(5,6);          

                               }

                              publicstaticvoid draw(int row,int col)

                              {

                                  for(int x=0;x<row;x++)

                                  {  

            for(int y=0;y<col;y++)

                                     {

                                         System.out.print("*");

                                     }

                                     System.out.println();

                                  }

          

                              }

         

            }

        运行结果:

                          ******

        ******

        ******

        ******

        ******



未完待续......


本文出自 “空谷幽兰” 博客,请务必保留此出处http://2489843.blog.51cto.com/2479843/1543141