首页 > 编程语言 >学校Java Week5

学校Java Week5

时间:2022-10-11 21:57:29浏览次数:76  
标签:Java int System 学校 static print Week5 public out

Week5

W5L1

Review

  • Control Flow
  • Condition Loops
  • counter Loops

For loops

Does the exact same thing with less code

for (int i = 0; i < 10; i++)
// initial value; condition:continue to loop; change

Nested For loops

for () {
    for(){
        ......
    }
}

//print 1-100
public class Forloops_1to100 {
    public static void main(String[] args) {
        for (int i = 1; i<11; i++) {
            for (int j = 1; j<11; j++) {
                System.out.print(i*j+"\t");
            }
            System.out.println();
        }
    }
}

Arrays

After created, the size can not be changed.

An array of primitives

int [] myArray = new int[5];
int [] myArray; // Declare
myArray = new int[5]; // Get a new array object with memory for 5 ints. Assign to myArray

Array Types

String[] myArray = {"a", "b"}
boolean[] myArray = {true, false}

String[] s = new String[10]; // default values: null
boolean[] b = new boolean[4]; // default values: false
int[] i = new int[10]; // default values: 0

W5L2 ex1

public class W5L2_ex1 {
    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 4, 5, 6};
        reverseInts(nums);
        printInts(nums);
    }

    public static void reverseInts(int[] nums) {
        int t = 0;
        int l = nums.length;
        for (int i=0; i<l/2; i++) {
            t = nums[i];
            nums[i] = nums[l-i-1];
            nums[l-i-1] = t;
        }
    }

    public static void printInts(int[] nums) {
        System.out.print("[");

        int l = nums.length;
        for (int i=0; i<l; i++) {
            if (i==0) {
                System.out.print(nums[i]);
            } else {
                System.out.print(", " + nums[i]);
            }
        }

        System.out.print("]");
    }
}

W5L2 ex2

public class W5L2_ex2 {
    public static void main(String[] args) {
        int [] nums = {1, 2, 3};
        nums = copyEven(nums);
        printInts(nums);
    }

    public static int[] copyEven(int[] nums) {
        int l = nums.length;
        int[] even = new int[(l+1)/2];
        int c = 0;

        for (int i=0; i<l; i++) {
            if (i%2 == 0) {
                even[c] = nums[i];
                c++;
            }
        }

        return even;
    }

    public static void printInts(int[] nums) {
        System.out.print("[");

        int l = nums.length;
        for (int i=0; i<l; i++) {
            if (i==0) {
                System.out.print(nums[i]);
            } else {
                System.out.print(", " + nums[i]);
            }
        }

        System.out.print("]");
    }
}

W5L2 ex3

public class W5L2_ex3 {
    // 两种方法均能解决
    public static void main(String[] args) {
        diamond(9);
        diamondArray(9);
    }

    public static void diamond(int n) {
        for (int i=1; i<n; i += 2) {
            int j = (n-i)/2;
            int t = j;
            int k = i;

            while (j>0) {
                if (j == (n-i)/2) {
                    System.out.print(".");
                } else {
                    System.out.print(" .");
                }
                j--;
            }

            while (k>0) {
                System.out.print(" *");
                k--;
            }

            while (t>0) {
                System.out.print(" .");
                t--;
            }

            System.out.println();
        }

        for (int i=n; i>=1; i -= 2) {
            int j = (n-i)/2;
            int t = j;
            int k = i;

            while (j>0) {
                if (j == (n-i)/2) {
                    System.out.print(".");
                } else {
                    System.out.print(" .");
                }
                j--;
            }

            while (k>0) {
                if (k == n) {
                    System.out.print("*");
                    k--;
                    while (k>0) {
                        System.out.print(" *");
                        k--;
                    }
                    break;
                }

                System.out.print(" *");
                k--;
            }

            while (t>0) {
                System.out.print(" .");
                t--;
            }

            System.out.println();
        }
    }

    public static void diamondArray(int n) {
        int[] indexArray = new int[n];
        for (int i=0; i<n; i+=2) {
            int t = i;
            int first = (n+2-i)/2 - 1;
            for (int c=0; c<=i; c++) {
                indexArray[first+c] = 1;
            }

            for (int j=0; j<n; j++) {
                if (indexArray[j] == 0) {
                    if (j==0) {
                        System.out.print(".");
                    } else {
                        System.out.print(" .");
                    }

                } else {
                    if (t==n-1) {
                        System.out.print("*");

                        while (t>0) {
                            System.out.print(" *");
                            t--;
                        }

                        break;
                    }
                    System.out.print(" *");
                }
            }

            System.out.println();
        }

        for (int i=n-2; i>0; i-=2) {
            int left_last = (n+2-i)/2 - 2;
            int right_first = (n+2+i)/2 - 1;

            for (int c=0; c<=left_last; c++) {
                indexArray[c] = 0;
            }

            for (int c=right_first; c<=n-1; c++) {
                indexArray[c] = 0;
            }

            for (int j=0; j<n; j++) {
                if (indexArray[j] == 0) {
                    if (j==0) {
                        System.out.print(".");
                    } else {
                        System.out.print(" .");
                    }

                } else {
                    System.out.print(" *");
                }
            }

            System.out.println();
        }
    }
}

W5L2 ex4

public class W5L2_ex4 {
    public static void main(String[] args) {
        printInts(arithSeries(4));
    }

    public static int[] arithSeries(int n) {
        int[] myArray = new int[n*(n + 1)/2];
        int index = 0;

        for (int i=1; i<=n; i++) {
            for (int j=1; j<=i; j++) {
                myArray[index] = j;
                index++;
            }
        }

        return myArray;
    }

    public static void printInts(int[] nums) {
        System.out.print("[");

        int l = nums.length;
        for (int i=0; i<l; i++) {
            if (i==0) {
                System.out.print(nums[i]);
            } else {
                System.out.print(", " + nums[i]);
            }
        }

        System.out.print("]");
    }
}

W5 CW1 5.1

public class W5_CW1_51 {
    public static void main(String[] args) {
        bandMatrix(8,0);
        bandMatrix(8,1);
        bandMatrix(8,2);
        bandMatrix(8,3);
        bandMatrix(8,4);
        bandMatrix(8,5);
        bandMatrix(8,6);
        bandMatrix(8,7);
    }

    public static void bandMatrix(int n, int width) {
        for (int i=0; i<n; i++) {
            for (int j=0; j<n; j++) {
                if (j == 0) {
                    int ij = i+j;
                    if (Math.abs(ij-2*i) <= width) {
                        System.out.print("*");
                    } else {
                        System.out.print(0);
                    }
                } else {
                    int ij = i+j;
                    if (Math.abs(ij-2*i) <= width) {
                        System.out.print("  *");
                    } else {
                        System.out.print("  " + 0);
                    }
                }
            }

            System.out.println();
        }
    }
}

W5 CW1 5.2

public class w5_cw1_52 {
    public static void main(String[] args) {
        int[] nums1 = {1, 2, 3, 4};
        printInts(evenOddSwap(nums1));
        int[] nums2 = {100, 25};
        printInts(evenOddSwap(nums2));
        int[] nums3 = {};
        printInts(evenOddSwap(nums3));
        int[] nums4 = {11, 55, 100, 200, 300, 7};
        printInts(evenOddSwap(nums4));
    }

    public static int[] evenOddSwap(int[] nums) {
        int l = nums.length;
        boolean[] flag = new boolean[l];
        int t = 0;
        for (int i=0; i<l; i++) {
            if (flag[i]) {
                continue;
            } else {
                flag[i] = true;
            }

            if (nums[i]%2 == 0) {
                for (int j=i; j<l; j++) {
                    if (nums[j]%2 == 1) {
                        t = nums[j];
                        nums[j] = nums[i];
                        nums[i] = t;
                        flag[j] = true;
                        break;
                    }
                }
            } else {
                for (int j=i; j<l; j++) {
                    if (nums[j]%2 == 0) {
                        t = nums[j];
                        nums[j] = nums[i];
                        nums[i] = t;
                        flag[j] = true;
                        break;
                    }
                }
            }
        }

        return nums;
    }

    public static void printInts(int[] nums) {
        System.out.print("[");

        int l = nums.length;
        for (int i=0; i<l; i++) {
            if (i==0) {
                System.out.print(nums[i]);
            } else {
                System.out.print(", " + nums[i]);
            }
        }

        System.out.print("]");
    }
}

标签:Java,int,System,学校,static,print,Week5,public,out
From: https://www.cnblogs.com/POLAYOR/p/16782721.html

相关文章

  • Java-Day05 while/do-while/多重嵌套例题/break/continue
    学习时间:2022/10/10-2022/10/11一、while循环1.结构:①初始化条件While(②循环条件[布尔类型]){③循环体;④迭代条件;}执行过程:①-②......
  • 新人小白学Java第一天笔记
    一、标识符1关键字2标识符注意(1)所有的标识符都应该以字母(A-Z或者a-z),美元符($),或者下划线(_)开始(2)首字符之后可以是字母(A-Z或者a-z),美元符($),或者下划线(_)或数字的任何字符......
  • ActiveMQ启动报错(端口被占用):java.lang.IllegalStateException
    ActiveMQ端口被异常占用这个问题太恶心了,启动Tomcat,默认的端口是8080,死活启动不了,看着报错信息就是端口被占用,网上一直搜索解决办法:netstat-aon|findstr"8080" 然......
  • Java的匿名内部类
    匿名内部类也就是没有名字的内部类 正因为没有名字,所以匿名内部类只能使用一次,它通常用来简化代码编写但使用匿名内部类还有个前提条件:必须继承一个父类或实现一个接口 实......
  • java中的泛型总结
    要我直接说出泛型是个what我还真讲不出来,这里先由一道问题引入:定义一个坐标点类,要求能保存各种类型的数据,如:整形,浮点型,和字符串类型既然变量类型起先不确定,那么很容易想到......
  • JAVA的内部类
    内部类不是很好理解,但说白了其实也就是一个类中还包含着另外一个类如同一个人是由大脑、肢体、器官等身体结果组成,而内部类相当于其中的某个器官之一,例如心脏:它也有自己的......
  • idea javaWeb tomcat报Driver ClassNotFoundException解决办法
    1.在WEB-INF目录下创建lib目录(如果没有的话),再接着将mysql-connection的jar报复制过来放到lib目录下  2.再点击file-ProjectStructure-Modules,接着点击右上角......
  • Javaweb学习 -----------------操作数据库中的数据
    1、添加数据 insertinto表名(列名1,列名2,...)values(值1,值2,...);//给指定列添加数据insertinto表名values(值1,值2,...);......
  • Javaweb学习 ------------------操作表
    1、查询表showtables;//查询当前数据库下所有表的名称desc表名称;//查询当前表中各个字段的结构属性2、创建表createtable表名(字段名1数据类......
  • 15、JAVA入门——封装
    目录​​ 一、封装​​​​      1、封装概述​​​​   2、封装的步骤​​​​二、Java里的包​​​​      1、包的概述​​​​      2、包的......