码迷,mamicode.com
首页 > 编程语言 > 详细

javase基础5

时间:2017-05-29 00:57:59      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:修饰符   需要   int   byte   值类型   int()   false   blog   ext   

1.方法格式

  

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

                   函数体;

                   return 返回值;

}

修饰符: public static 等

 返回值类型:用于限定返回值的数据类型

 方法名:一个名字,为了方便我们调用方法

 参数类型:用于接收调用方法时传入的数据的类型

 参数名:用于接收调用方法时传入的数据的变量

 方法体:完成特定功能的代码

 return:结束方法,把返回值带个调用者

写一个方法需要明确:参数列表和返回值类型

2.方法获取两个数的最大值

  

import java.util.Scanner;

/*
 * 键盘录入两个数据,返回两个数中的较大值
 * 
 * 两个明确:
 *         返回值类型:int
 *         参数列表:int a,int b
 */
public class Demo1{
    // 返回两个数中的较大值
    public static int getMax(int a, int b) {
        if (a > b) {
            return a;
        } else {
            return b;
        }
    }
    
    public static void main(String[] args) {
        //创建对象
        Scanner sc = new Scanner(System.in);
        
        //接收数据
        System.out.println("请输入第一个数据:");
        int x = sc.nextInt();
        
        System.out.println("请输入第二个数据:");
        int y = sc.nextInt();
        
        //调用方法
        int max = getMax(x,y);
        System.out.println("max:"+max);
    }
}

3.判断俩个数是否相等

  

public class Demo2{
    //比较两个数是否相等
    public static boolean compare(int a,int b){
        if(a==b){
            return true;
        }else {
            return false;
        }
    }
    
    public static void main(String[] args) {
        //创建对象
        Scanner sc = new Scanner(System.in);
        
        //接收数据
        System.out.println("请输入第一个数据:");
        int a = sc.nextInt();
        
        System.out.println("请输入第二个数据:");
        int b = sc.nextInt();
        
        //调用方法
        boolean flag = compare(a,b);
        System.out.println("flag:"+flag);
    }
}

4.获取三个数中的最大值

// 返回三个数中的最大值
    public static int getMax(int a, int b, int c) {
        if (a > b) {
            if (a > c) {
                return a;
            } else {
                return c;
            }
        } else {
            if (b > c) {
                return b;
            } else {
                return c;
            }
        }
    }
    
    public static void main(String[] args) {
        //创建对象
        Scanner sc = new Scanner(System.in);
        
        //接收数据
        System.out.println("请输入第一个数据:");
        int a = sc.nextInt();
        
        System.out.println("请输入第二个数据:");
        int b = sc.nextInt();
        
        System.out.println("请输入第三个数据:");
        int c = sc.nextInt();
        
        //调用方法
        int max = getMax(a,b,c);
        System.out.println("max:"+max);
    }
}

5.打印一到n的数据

public class Demo4{
    //在控制台打印1到该数据n的值
    public static void printNumber(int n) {
        for(int x=1; x<=n; x++) {
            System.out.println(x);
        }
    }
    
    public static void main(String[] args) {
        printNumber(10);
        System.out.println("-------------------");
        printNumber(100);
    }
}

6.打印水仙花数

public class Demo5{
    //把所有的水仙花数打印在控制台
    public static void printFlower() {
        for(int x=100; x<1000; x++) {
            int ge = x%10;
            int shi = x/10%10;
            int bai = x/10/10%10;
            
            if((ge*ge*ge+shi*shi*shi+bai*bai*bai) == x){
                System.out.println(x);
            }
        }
    }
    
    public static void main(String[] args) {
        printFlower();
    }
}

7.方法重载

  在同一个类中,允许存在一个以上的同名方法,只要它们的参数个数或者参数类型不同即可

  特点:与返回值类型无关,只看方法名和参数列表

案例:

/*
 * 比较两个数据是否相等。参数类型分别为两个byte类型,两个short类型,两个int类型,两个long类型,
 *     并在main方法中进行测试
 */
public class Demo6{
    public static void main(String[] args) {
        // 调用
        System.out.println(compare(10, 20));
        System.out.println("-------------");
        System.out.println(compare((byte)10, (byte)20));
        System.out.println("-------------");
        System.out.println(compare((short)10, (short)20));
        System.out.println("-------------");
        //System.out.println(compare((long)10, (long)20));
        System.out.println(compare(10L, 20L));
    }

    // 两个byte类型的
    public static boolean compare(byte a, byte b) {
        System.out.println("byte");
        // 第一种写法
        // boolean flag = a==b?true:false;
        // return flag;
        // 第二种写法
        // boolean flag = a == b;
        // return flag;
        // 第三种写法
        return a == b;
    }

    // 两个short类型的
    public static boolean compare(short a, short b) {
        System.out.println("short");
        return a == b;
    }

    // 两个int类型的
    public static boolean compare(int a, int b) {
        System.out.println("int");
        return a == b;
    }

    // 两个long类型的
    public static boolean compare(long a, long b) {
        System.out.println("long");
        return a == b;
    }
}

8.方法中参数的传递

  方法的参数是基本数据类型的时候:形式参数的改变不影响实际参数

  形式参数:用于接收实际数据的变量

  实际参数:实际参与运算的变量

当形式参数是引用数据类型时:

  

/*
 * 方法的参数是引用类型:
 *         形式参数的改变直接影响实际参数
 */
public class Demo7{
    public static void main(String[] args) {
        // 定义数组
        int[] arr = { 1, 2, 3, 4, 5 };
        // 遍历数组
        for (int x = 0; x < arr.length; x++) {
            System.out.println(arr[x]);
        }
        System.out.println("----------------");
        change(arr);
        for (int x = 0; x < arr.length; x++) {
            System.out.println(arr[x]);
        }
    }

    public static void change(int[] arr) {
        for (int x = 0; x < arr.length; x++) {
            // 如果元素是偶数,值就变为以前的2倍
            if (arr[x] % 2 == 0) {
                arr[x] *= 2;
            }
        }
    }
}

9.数组元素求和

/*
 * 写一个方法,用于对数组进行求和,并调用方法。
 */
public class Demo8 {
    public static void main(String[] args) {
        // 定义数组
        int[] arr = { 1, 2, 3, 4, 5 };
        int sum = sum(arr);
        
        System.out.println("sum:"+sum);
    }
    
    //第一一个求和方法
    public static int sum(int[] arr) {
        int sum = 0;
        
        for(int x=0; x<arr.length; x++) {
            sum += arr[x];
        }
        
        return sum;
    }
}

 

javase基础5

标签:修饰符   需要   int   byte   值类型   int()   false   blog   ext   

原文地址:http://www.cnblogs.com/learnjfm/p/6917070.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!