JAVA方法

什么是方法

Scanner sc=new Scanner(System.in);

sc.nextInt();这就是方法

方法的声明和调用

1声明

访问修饰符 返回类型 方法名(参数列表){

     方法体

}如

访问修饰符:允许访问的权限;

方法名:除了满足标识符,第一个单词小写其他第一个字母都

无参无返回值方法

列:一行打印输出一串星号 

无参带返回值方法

求长方形的面积:

带参无返回值方法

带参带返回值方法

数组作为方法参数

查找数组元素的值

  

方法2

 

方法重载

方法名相同,参数列表不同

实现int float arr和的方法

参数的传递问题

在JAVA中参数无法换回2个及以上的返回值

主方法中方法的调用需要对象而在其他中可以直接使用如

数组中传值的问题

和普通变量传值不一样!!!所以基本类型传值都一样;所有引用类型传值也一样;

可变参数列表

查找函数

会出这样的错误

可以把数组的值传给可变参数但是不能把可变参数传给数组;在方法重载中可变参数所在的列表是最后被访问的。

单行注释为//,多行注释为/*   */,帮助文档注释为/** 回车

在命令控制符中输入javadoc -d- 后面还有。。。。

 方法的调试

 作业

需求:定义一个类,对数组中的数据进行管理(增,删,改,查)

 

package JAVA;
import java.util.Scanner;
public class shuJuGuanLi {
   public int[] insertData()
   {
    int intArray[]=new int[10];
    for(int i=0;i<intArray.length-1;i++)
    {
     System.out.println("请输入数字");
     Scanner sc=new Scanner(System.in);
     intArray[i]=sc.nextInt();
    }
    return intArray;
   }
   public void showData(int[] a,int length)
   {
    for(int i:a)
    {
     System.out.print(i+" ");
    }
   }
   public void insertAtArray(int[] a)
   {
    System.out.println("请输入插入的数据大小为");
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    System.out.println("请输入插入的数据位置为");
    Scanner sc1=new Scanner(System.in);
    int k=sc1.nextInt();
    for(int i=0;i<a.length;i++)
    {
     if(k==i)
     {
      for(int j=a.length-1;j>=i-1;j--)
      {
       a[j]=a[j-1];
      }
      a[i-1]=n;
     }
    }
   }
   public void divThree(int[] a)
   {
    for(int i=0;i<a.length;i++)
    {
     if(a[i]%3==0)
     {
      System.out.print(a[i]+" ");
     }
    }
   }
   public void notice()
   {
    System.out.println("请输入对应数字进行操作");
   }
 public static void main(String[] args) {
  shuJuGuanLi shuju=new shuJuGuanLi();
  int a[]=new int[10];
        for(int i=1;;i++)
        {
         shuju.notice();
         Scanner sc=new Scanner(System.in);
         int num=sc.nextInt();
         switch(num)
         {
         case 1:a=shuju.insertData();break;
         case 2:shuju.showData(a, a.length);break;
         case 3:shuju.insertAtArray(a);break;
         case 4:shuju.divThree(a);;break;
         }
         if(num==0)
{
System.out.print("退出");
break;}
        }
 }
}
原文地址:https://www.cnblogs.com/xiaoruirui/p/10684019.html