方法和数组

这篇博客涵盖了Java的基础语法,包括使用`break`和`continue`控制循环流程,以及标签在多层循环中的应用。还展示了如何打印三角形、实现方法、重载以及处理数组。此外,介绍了递归和数组操作,如求和、找最大值以及数组的反转。这些实例深入浅出地展示了Java编程的核心概念。
摘要由CSDN通过智能技术生成

break&continue

package com.study.struct;

public class BreakDemo {
    public static void main(String[] args) {
        int i = 0;
        while (i < 100) {
            i++;
            System.out.println(i);
            if (i == 30) {
                break;
            }
        }
        System.out.println("123");
    }
}

package com.study.struct;

public class ContinueDemo {
    public static void main(String[] args) {
        int i = 0;
        while (i < 100) {
            i++;
            if (i % 10 == 0) {
                System.out.println();
                continue;
            }
            System.out.print(i + "\t");
        }
    }
}

package com.study.struct;

public class LableDemo {
    public static void main(String[] args) {
        //打印101-150之间所有的质数(只能被1和数字本身整除)
        int count = 0;
        outer:
        for (int i = 101; i <= 150; i++) {
            for (int j = 2; j < i / 2; j++) {
                if (i % j == 0) {
                    continue outer;
                }
            }
            System.out.println(i);
        }


    }
}

打印三角形

package com.study.struct;

public class TestDemo01 {
    public static void main(String[] args) {
        // 打印三角形 5行

        /**
         * 11111*
         * 1111**
         * 111***
         * 11****
         * 1*****
         *
         *
         * 11111**11111
         * 1111****1111
         * 111******111
         * 11********11
         * 1**********1
         *
         *
         * 11111*11111
         * 1111***1111
         * 111*****111
         * 11*******11
         * 1*********1
         */
        for (int i = 1; i <= 5; i++) {
            for (int j = 5; j >= i; j--) {
                System.out.print("1");
            }
            for (int x = 1; x <= i; x++) {
                System.out.print("*");
            }
            for (int j = 1; j < i; j++) {
                System.out.print("*");
            }
            for (int x = 5; x >= i; x--) {
                System.out.print("1");
            }
            System.out.println();

        }
    }
}

方法

package com.study.method;

public class Demo01 {
    //main方法
    public static void main(String[] args) {
        //    int sum = add(1, 2);
        //  System.out.println(sum);
        test();
    }

    //加法                 形参
    public static int add(int x, int y) {
        int result = 0;
        result = x + y;
        return result;
    }

    public static void test() {
        for (int i = 0; i <= 1000; i++) {
            if (i % 5 == 0 && i != 0) {
                System.out.print(i + "\t");
                if (i % (5 * 3) == 0) {
                    System.out.print("\n");
                }
            }

        }
    }
}

方法重载

package com.study.method;

public class Demo02 {
    public static void main(String[] args) {
        // 方法的重载:名称相同,参数列表不同:个数,顺序,类型
        //  int max = max(10, 20);
        double compare = compare(30.0, 30.0);
        System.out.println(compare);
    }

    //比大小
    public static int compare(int x, int y) {
        int z = x > y ? x : y;
        return z;
    }

    public static double compare(double x, double y) {
        double z = x > y ? x : y;
        return z;
    }

    public static int max(int x, int y) {
        int max = -1;
        if (x > y) {
            max = x;
        } else {
            max = y;
        }
        return max;
    }
}

package com.study.method;

public class Demo03 {
    public static void main(String[] args) {
        //args.length 数组长度
        for (int i = 0; i < args.length; i++) {
            System.out.println("args["+i+"]"+args[i]);
        }
    }
}

可变参数

package com.study.method;

public class Demo04 {
    public static void main(String[] args) {
        Demo04 demo04 = new Demo04();
        demo04.test(10, 20, 30);
    }

    public void test(double x, int... i) {
        // System.out.println(i[0]);
        for (int y : i
        ) {
            System.out.println(y);
        }
    }
}

package com.study.method;

public class Demo05 {
    public static void main(String[] args) {
        // printMax(10.0,20.0,4.0,6.0,0.0,50.0);
        printMax(new double[]{1, 2, 4, 6, 8});
    }

    public static void printMax(double... numbers) {
        if (numbers.length == 0) {
            System.out.println("no argument passed");
            return;
        }
        double result = numbers[0];
        for (double x : numbers
        ) {
            if (x > result) {
                result = x;
            }
        }
        System.out.println("the max value is: " + result);

    }
}

递归

package com.study.method;

public class Demo05 {
    public static void main(String[] args) {
        // printMax(10.0,20.0,4.0,6.0,0.0,50.0);
        printMax(new double[]{1, 2, 4, 6, 8});
    }

    public static void printMax(double... numbers) {
        if (numbers.length == 0) {
            System.out.println("no argument passed");
            return;
        }
        double result = numbers[0];
        for (double x : numbers
        ) {
            if (x > result) {
                result = x;
            }
        }
        System.out.println("the max value is: " + result);

    }
}

数组

package com.study.arrays;

public class ArrayDemo01 {
    public static void main(String[] args) {
        int[] numbers;// 定义  首选
        int numbers2[] = new int[10];

        numbers = new int[10]; //这里面可以存放10个int类型的数字 创建一个数组
        // 给数组中的元素赋值
        numbers[0] = 1;
        for (int i = 0; i < numbers.length; i++) {
            numbers[i] = i + 1;
            System.out.println(numbers[i]);
        }
        // 求和
        int sum = 0;
        for (int x : numbers
        ) {
            sum += x;
        }
        System.out.println(sum);


    }
}

package com.study.arrays;

public class ArrayDemo02 {
    public static void main(String[] args) {
        /**
         * 数组的基本特点:
         * 1 其长度是确定的。数组一旦被创建,他的长度就是不可以改变的。
         * 2 其元素必须是相同的类型,不允许出现混合类型
         * 3 数组的元素可以试试任何类型,包括基本数据类型和引用数据类型
         * 4 数组的变量属于引用类型,数组也可以看成对象,数组中的每个元素相当于给对象的成员变量
         *   数组本身就是对象,java中对象存放方在堆内存中,因此数组无论存放基本数据类型,还是引用数据类型
         *   数组对象本身是在堆中的;
         */
        /**
         * 下标的合法区间:【0,length-1】 如果越界就会报错
         */
        /**
         * 小结:
         * 1 数组是相同的数据类型(数据类型可以是任何数据类型)的有序集合
         * 2 数组也是对象。数组的元素相当于对象的成员变量
         * 3 数组的长度是固定的,不可以变的。如果越界会报ArrayIndexOutofBounds
         */

        // 静态初始化  创建+赋值
        int[] a = {1, 2, 3, 45, 6};
        System.out.println(a[0]);
        Man[] man = {new Man(), new Man(), new Man()};
        //动态初始化 包含默认初始化
        int[] b = new int[10];
        b[0] = 1;
        System.out.println(b[0]);
    }
}

package com.study.arrays;

public class ArrayDemo03 {
    public static void main(String[] args) {
        int[] num = {1, 2, 3, 4, 5};
        //打印全部的数组元素
        for (int i = 0; i < num.length; i++) {
            System.out.println(num[i]);
        }
        //计算所有的元素的和
        int sum = 0;
        for (int i = 0; i < num.length; i++) {
            sum += num[i];
        }
        System.out.println(sum);
        System.out.println("================");
        // 查找最大元素
        for (int i = 1; i < num.length; i++) {
            if (num[0] < num[i]) {
                num[0] = num[i];
            }
        }
        System.out.println(num[0]);
    }
}

package com.study.arrays;

public class ArrayDemo04 {
    public static void main(String[] args) {
        int[] arrays = {1, 2, 3, 4, 5};
        /*for (int array : arrays) {
            System.out.println(array);
        }*/
        //print(arrays);
        reverse1(arrays);
    }

    // 打印数组元素  数组参数
    public static void print(int[] array) {
        for (int i : array) {
            System.out.println(i);
        }
    }

    //翻转数组
    public static int[] reverse(int[] arrays) {
        int x = 0;
        for (int i = 0, j = arrays.length - 1; i < j; i++, j--) {
            x = arrays[i];
            arrays[i] = arrays[j];
            arrays[j] = x;
        }
        for (int array : arrays) {
            System.out.println(array);
        }
        return arrays;
    }

    public static int[] reverse1(int[] arrays) {
        int[] result = new int[arrays.length];
        for (int i = 0, j = arrays.length - 1; i < arrays.length; i++, j--) {
            result[i] = arrays[j];
        }
        for (int i : result) {
            System.out.println(i);
        }
        return result;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值