带参数返回值的方法

|-----面向对象思想  main(){//调试  传递值}

 

publicclass Calculator {

      publicvoid cal(int one,int two,char sign){//形参

           double result=0;

           //根据传递过来的sign符号来做加减乘除

           switch(sign){

                 case'+':

                      result=one+two;

                      break;

                 case'-':

                      result=one-two;

                      break;

                 case'*':

                      result=one*two;

                      break;

                 case'/':

                      result=one/two;

                      break;

                 default:

                      System.out.println("输入的运算符有误!");

                      break;

           }

           System.out.println("结果:"+result);

      }

}

 

|----主方法

publicstaticvoid main(String[] args) {

        // TODO Auto-generated method stub

        Calculator cc=new Calculator();

        Scanner sc=new Scanner(System.in);//扫描仪

        System.out.println("请输入第1个整数:");

        int one=sc.nextInt();

        System.out.println("请输入第2个整数:");

        int two=sc.nextInt();

        System.out.println("请输入运算符(+-*/):");

        char sign=sc.next().charAt(0);

        cc.cal(one, two, sign);//实参

    }

 

 

publicclass Computer {

    String cpu;

    String motherborad;

    String view;

    String data;

    String ram;

    publicvoid showInfo(){

        System.out.println("cpu:"+this.cpu+"\t主板:"+this.motherborad+"\t显示器:"+this.view+"\t硬盘:"+this.data+"\t内存:"+this.ram);

    }

}

 

 

publicstaticvoid main(String[] args) {

           // TODO Auto-generated method stub

           Computer cc=new Computer();

           Scanner sc=new Scanner(System.in);

           System.out.println("请输入cpu的型号:");

           cc.cpu=sc.next();

           System.out.println("请输入主板:");

           cc.motherborad=sc.next();

           System.out.println("请输入显示器:");

           cc.view=sc.next();

           System.out.println("请输入硬盘:");

           cc.data=sc.next();

           System.out.println("请输入内存:");

           cc.ram=sc.next();

           cc.showInfo();

      }

 

 

|-----void:无返回值  调用方法后不用去管理方法!

 

|-----带个返回值  返回对象  返回接口  原理:如何实现返回(return?)

带参数返回值的方法

|-----1:老和尚叫小和尚去买菜,但是没有给小和尚钱!

带参数返回值的方法

 带参数返回值的方法

|-----在类中,有无参,返回值的函数

带参数返回值的方法

u  |----2:(给钱)从键盘接收三门课分数,计算三门课的平均分和总成绩,编写成绩计算类实现功能

带参数返回值的方法

|----思考:one,two,three能否放在数组中传递?

 带参数返回值的方法

|----1:成员变量:只要是在类中定义的方法都可以去改变,但是局部变量,仅限于当前的方法体可以改变!

2:初始值不同

Java会给成员变量一个初始值

Java不会给局部变量赋予初始值

|-----思考:把数组当成参数进行传递

带参数返回值的方法

|------带参数返回值的方法

<访问修饰符 public>  返回类型(int doubleString)  <方法名>(<形式参数列表>) {

               //方法的主体

}

 

 

  在保存了多个学生姓名的数组中,指定查找区间,查找某个学生姓名并显示是否查找成功 true  false

 设计方法,通过传递三个参数(开始位置、结束位置、查找的姓名)来实现

带参数返回值的方法

 

|-----形参:数组  对象(类)

 带参数返回值的方法

 

|-----小结:如果返回类型:类,那么这个类中可以访问的属性,方法都可调用!

|-----思考:在StudentBiz类去调用Student类中平均分的方法

publicvoid getAvg(Student stu){

        //去调用Student类中的平均分方法

    }

 

publicdouble avg(){

           return 0;

      }

 带参数返回值的方法

带参数返回值的方法

|-----对象数组

|-----两重概念  对象 + 数组

publicclass Student {

      intjavaScore;

      inthtmlScore;

      //int cScore;

}

 

publicclass StudentBiz {

      //声明一个对象数组

      Student[] stu=new Student[3];

      publicvoid add(){

           //1:学会给对象数组赋值  Student类给每一个数组

           stu[0]=new Student();

           //stu[0]当成实例化对象来理解

           stu[0].javaScore=21;

           stu[0].htmlScore=22;

          

           stu[1]=new Student();

           //stu[0]当成实例化对象来理解

           stu[1].javaScore=55;

           stu[1].htmlScore=56;

          

           stu[2]=new Student();

           //stu[0]当成实例化对象来理解

           stu[2].javaScore=77;

           stu[2].htmlScore=78;

      }

      //写活

      publicvoid add2(){

           Scanner sc=new Scanner(System.in);

           for(int i=0;i<stu.length;i++){

                 System.out.println("请输入第"+(i+1)+"个学生的成绩:");

                 stu[i]=new Student();

                 stu[i].javaScore=sc.nextInt();

                 stu[i].htmlScore=sc.nextInt();

           }

      }

      publicvoid show(){

           //add();

           add2();

           //显示对象数组?

           for(int i=0;i<stu.length;i++){

                 System.out.println(stu[i].javaScore+"\t"+stu[i].htmlScore);

           }

      }

}