java入门案例:方法练习和数组的操作

import java.util.Scanner;

/*
缩进的是完成的:
    0.设计一个方法 用来画星星 倒三角行数不确定 方向也不确定
    *1.设计一个方法 用来交换两个数组元素 a{1,2,3,4}b{5,6,7,8}
    *2.设计一个方法 用来交换一个数组(头尾互换)
    *3.设计一个方法 用来寻找数组中的极值(最大值 或 最小值)
    *4.设计一个方法 用来找寻给定的元素是否在数组内存在(Scanner输入一个)
    5.设计一个方法 用来合并两个数组
    6.设计一个方法 用来将一个数组按照最大值位置拆分
*/
public class ArrayOperations {
    public static void main(String[] args) {
        //1画星星
        System.out.print("\n" + "你要画几行:");
        int x1 = new Scanner(System.in).nextInt();
        System.out.print("1:正,0:反:");
        int b = new Scanner(System.in).nextInt();
        DarwStar(x1,b);

        //2数组交换
        System.out.print("\n" + "数组长度:");
        int x2 = new Scanner(System.in).nextInt();
        Arr(x2);

        //3数组反序排列
        System.out.print("\n" + "数组长度:");
        int x3 = new Scanner(System.in).nextInt();
        Arr1(x3);

        //4寻找最大最小值
        System.out.print("\n" + "数组长度:");
        int x4 = new Scanner(System.in).nextInt();
        Arr2(x4);

        //5查找元素
        System.out.print("\n" + "数组长度:");
        int x5 = new Scanner(System.in).nextInt();
        System.out.print("要找的元素:");
        int c = new Scanner(System.in).nextInt();
        Arr3(x5, c);

        //6合并两个数组
        System.out.print("\n" + "数组长度:");
        int x6 = new Scanner(System.in).nextInt();
        Arr4(x6);

        //7按最大值为分界拆分数组
        System.out.print("\n" + "数组长度:");
        int x7 = new Scanner(System.in).nextInt();
        Arr5(x7);
    }
    //7按最大值为分界拆分数组
    public static void Arr5(int x) {
        int[] Array = new int[x];
        for (int i = 0; i < x; i++) {
            System.out.print("Array[" + i + "]元素:");
            Array[i] = new Scanner(System.in).nextInt();
        }
        //获取最大值所在位置的索引
        int max = Array[0];
        int a = 0;
        for (int i = 0; i < Array.length; i++) {
            for (int j = 0; j < Array.length; j++) {
                if (max < Array[j]) {
                    max = Array[j];
                    a=j;
                }
            }
        }
        //System.out.println(a);
        //创建新数组,根据获取的最大值索引确定新数组的长度
        int[] Array1 = new int[a];
        int[] Array2 = new int[Array.length-a-1];
        //为新数组填充元素并遍历
        System.out.print("Array1:");
        for (int i = 0; i < Array.length; i++) {
            if(i<a){
                Array1[i] = Array[i];
                System.out.print(Array1[i] + "\t");
            }else if(i>a){
                Array2[i-a-1] = Array[i];
            }
        }
        System.out.print("\nArray2:");
        for(int i:Array2){
            System.out.print(i + "\t");
        }
        Array = null;//清空原数组
    }

    //6合并两个数组
    public static void Arr4(int x) {
        int[] Array1 = new int[x];
        int[] Array2 = new int[x];
        System.out.print("Array1:");
        for (int i = 0; i < x; i++) {
            Array1[i] = (i * 3 + 6);
            System.out.print(Array1[i] + "\t");//遍历Array1
            Array2[i] = (i * 5 + 3);
        }
        System.out.print("\n" + "Array2:");
        for (int i : Array2) {
            System.out.print(i + "\t");
        }
        System.out.print("\n新数组" + "\n" + "Array3:");
        int[] Array3 = new int[Array1.length + Array2.length];
        for (int i = 0; i < Array3.length; i++) {
            if (i < Array1.length) {
                Array3[i] = Array1[i];
            } else {
                Array3[i] = Array2[i - Array1.length];
            }
        }
        for (int i : Array3) {
            System.out.print(i + "\t");
        }
    }

    //5查找元素
    public static void Arr3(int x, int y) {
        int[] Array = new int[x];
        for (int i = 0; i < x; i++) {
            System.out.print("Array[" + i + "]元素:");
            Array[i] = new Scanner(System.in).nextInt();
        }
        boolean b = true;
        for (int i = 0; i < Array.length; i++) {
            if (y == Array[i]) {
                System.out.println(y + "是Array数组中第" + (i + 1) + "个元素。");
                b = false;
                break;
            }
        }
        if (b) {
            System.out.println("Array数组中不存在这个元素。");
        }
    }

    //4寻找最大最小值
    public static void Arr2(int x) {
        int[] Array = new int[x];
        for (int i = 0; i < x; i++) {
            System.out.print("Array[" + i + "]元素:");
            Array[i] = new Scanner(System.in).nextInt();
        }
        int max = Array[0];
        int min = Array[0];
        for (int i = 0; i < Array.length; i++) {
            for (int j = 0; j < Array.length; j++) {
                if (max < Array[j]) {
                    max = Array[j];
                }
                if (min > Array[j]) {
                    min = Array[j];
                }
            }
        }
        System.out.println("\n数组Array中最大值是:" + max + "最小值是:" + min);
    }

    //3数组反序排列
    public static void Arr1(int x) {
        System.out.print("初始状态" + "\n" + "Array:");
        int[] Array1 = new int[x];
        for (int i = 0; i < x; i++) {
            Array1[i] = (i);
            System.out.print(Array1[i] + "\t");//遍历Array1
        }
        System.out.print("\n换位状态" + "\n" + "Array:");
        for (int i = 0; i < Array1.length / 2; i++) {
            int y = Array1[i];
            Array1[i] = Array1[Array1.length - i - 1];
            Array1[Array1.length - i - 1] = y;
        }
        for (int i : Array1) {
            System.out.print(i + "\t");
        }
    }

    //2数组交换
    public static void Arr(int x) {
        System.out.print("初始状态" + "\n" + "Array1:");
        int[] Array1 = new int[x];
        int[] Array2 = new int[x];
        for (int i = 0; i < x; i++) {
            Array1[i] = (i);
            System.out.print(Array1[i] + "\t");//遍历Array1
            Array2[i] = (x + i);
        }
        System.out.print("\n" + "Array2:");
        for (int i : Array2) {
            System.out.print(i + "\t");
        }
        System.out.print("\n换位状态" + "\n" + "Array1:");
        for (int i = 0; i < Array1.length; i++) {
            Array1[i] = Array1[i] ^ Array2[i];
            Array2[i] = Array1[i] ^ Array2[i];
            Array1[i] = Array1[i] ^ Array2[i];
            System.out.print(Array1[i] + "\t");//遍历Array1
        }
        System.out.print("\n" + "Array2:");
        for (int i : Array2) {
            System.out.print(i + "\t");
        }
    }

    //1画星星方法
    public static void DarwStar(int a, int b) {
        for (int x = 0; x < a; x++) {
            if (b == 0) {
                for (int y = 0; y < x; y++) {
                    System.out.print(" ");
                }
            }
            for (int y = 0; y < a - x; y++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

劉鎠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值