Java 方法的使用 第一个代码块是基本的void,第二个是return一些值

public class Lecture {
    public static void main (String[] args){
        //main method is static, then main can only call static methods
    String a = "os.version";
    String name = "user.name";
    version();
    info("os.name"); //to call method and to pass value
    info(a);
    info(name); //This variable name "name" is belonging to main method, the other one is belonging to another method.
                //Which is a local variable


    square();
    square(8, 12);
    }



    //display Java Version no parameter no return
    public static void version(){
        System.out.println(System.getProperty("java.version"));
    }
    //display Java System Properties - one parameter
    public static void info(String name){
        System.out.println(System.getProperty(name));
    }

    //display square number from 1 - 10
    public static void square(){
        final int MAX = 10;
        for (int i=1; i<=MAX; i++){
            System.out.print(i*i + " ");
        }
        System.out.println("");
    }

    // Precondition: states what is assumed to be true when the method is called.
    // Postcondition: describes the effect of the method call. It describes the value returned by the method.


    // display square number from A - B (A and B are the parameters)
    public static void square(int A, int B){ // A < B = Precondition
        for (int i=A; i<=B; i++){
            System.out.print(i*i + " ");  //Postcondition
        }
        System.out.println("");
    }









}
import java.util.Random;

public class Lecture2 {
    public static  void main(String[] args){
        System.out.println(getAlphabetUM("UMUMUWIEIAJWDIIDUM"));


        //pass by value(只传值)
        int num = 10;
        special(num);
        System.out.println(num);

        // reference type - array (类似软连接,array是一个整体的data,软连接不一样而已,改软连接内的 值相当于直接改data)
//        int [] original = {1,2,3,4,5};
//        int [] newnum = original;
//        newnum[1] = 88;
//        System.out.println(original[1]);
        int[] original = {1,2,3,4,5,};
        special(original);
        System.out.println(original[1]);

        //how to make a copy of array?
        int[] copyOriginal = original.clone();
        copyOriginal[4] = 100;
        System.out.println(original[4]); //original的value不变,变的是copyOriginal




        //return the number of odd numbers from N random numbers from A - B
        int N = 10;
        int[] number = new int[N];

        int cntodd = getOddrandomNumber(number, 10, 50);
        System.out.print("The random numbers are : " );
        for (int value : number) {
            System.out.print(value + " ");
        }
        System.out.println("\nThe number of odd number : " + cntodd);



        int[] cntoddeven = getOddEven(number, 10, 50);
        System.out.print("The random numbers are : ");
        for (int value: number){
            System.out.print(value + " ");
        }
        System.out.println("\nThe number of odd : " + cntoddeven[0]);
        System.out.println("The number of even : " + cntoddeven[1]);
    }






    //return the number of alphabet U and M
    public static int getAlphabetUM(String a){
        int cnt=0;
        for (int i=0;i<a.length();i++){
            if (a.charAt(i) =='U' || a.charAt(i)=='M'){
                cnt++;
            }
        }
        return cnt;
    }


    //return the perimeter of rectangle (width and height are parameters)
    public static double getPerimeter(double width,double height){
        return (width + height) *2;
    }

    //return the perimeter of circle (radius is parameter)
    public static double getPerimeter(double radius){
        return 2*Math.PI*radius;
    }



    //to show that special(num) in main only send value to special. it wont change value of num
    public static void special(int num){
        num = 99;
    }


    public static void special(int[] num){
        num[1] = 77;
    }



    //return the number of odd numbers from N random numbers from A - B
    public static int getOddrandomNumber(int[] num, int A, int B){
        Random r = new Random();
        int cntOdd=0;
        for (int i=0;i<num.length;i++){
            num[i] = (r.nextInt(B-A+1)+A);
            if (num[i]%2==1){
                cntOdd++;
            }
        }
        return cntOdd;
    }

    // return the number of odd and even number from N random numbers from A - B
    public static int[] getOddEven(int[] num, int A, int B){
        Random r = new Random();
        int[] cnt = new int[2];
        for (int i=0;i<num.length;i++){
            num[i] = (r.nextInt(B-A+1)+A);
            if (num[i]%2==1){
                cnt[0]++;
            }else{
                cnt[1]++;
            }
        }
        return cnt;

    }






}

发表评论