guyanqing преди 10 месеца
родител
ревизия
544904da58

+ 70 - 0
src/main/java/com/sf/day03/ArrayTest.java

@@ -0,0 +1,70 @@
+package com.sf.day03;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.Arrays;
+
+/**
+ * 一维数组
+ */
+public class ArrayTest {
+    /**
+     * 一维数组的定义;
+     * int[] arr;
+     * int arr[];
+     * Sting[] str;
+     * Double[] num;
+     * User[] users;
+     */
+    @Test
+    public void t1(){
+        //静态初始化
+        int[] arr = new int[]{1,2,3,4};
+        System.out.println(arr.length);
+        int[] arr1;
+        arr1 = new int[]{1,2,3,4};
+
+//        int[] arr2;
+//        arr2 = {1,2,3,4};
+        int[] arr3 = {1,2,3,4,5};
+    }
+
+
+    /**
+     * 数组元素的获取和遍历
+     */
+    @Test
+    public void t2(){
+       String[] str = new String[]{"zs","ls","ww","zl"};
+       //数组获取元素   通过下标进行获取  0   1    2    3
+        // 结构   str[index]
+//        System.out.println(str[2]);
+        //for 遍历
+//         for (int i = 0;i<str.length;i++){
+//             System.out.println(str[i]);
+//         }
+         str[2] = "qq";
+
+         // 快捷键   数字.for    itar
+        for (int i = 0;i<str.length;i++){
+            System.out.println(str[i]);
+        }
+        System.out.println("========================");
+        System.out.println(Arrays.toString(str));
+
+        /**
+         *  for
+         *  for(String s  :  str){
+         *
+         *  }
+         *
+         *  str:要是遍历的集合/数组
+         *  s:数组中的每一个元素
+         *  String :数组中的每一个元素的数据类型
+         *  快捷键   : iter
+         */
+        for (String s : str) {
+            System.out.println(s);
+        }
+    }
+}

+ 89 - 0
src/main/java/com/sf/day03/ArrayWorker.java

@@ -0,0 +1,89 @@
+package com.sf.day03;
+
+import org.junit.jupiter.api.Test;
+
+import java.net.SocketTimeoutException;
+
+/**
+ * 数组练习题
+ */
+public class ArrayWorker {
+    /**
+     * 数组统计:求总和、均值
+     * 求数组元素的总乘积
+     */
+    @Test
+    public void t1(){
+        int[] arr = {4,5,6,1,9};
+        int sum = 0;
+        int result = 1;
+        double avg = 0.0;
+        //sum   avg
+        for (int i = 0;i<arr.length;i++){
+          sum +=  arr[i];
+            result *= arr[i];
+        }
+        System.out.println(sum);
+        avg =sum/arr.length;
+        System.out.println(avg);
+        System.out.println(result);
+    }
+
+    /**
+     * 求数组元素的最大值
+     */
+    @Test
+    public void t2(){
+        int[] arr = {4,5,6,1,9};
+        int max = arr[0];
+        for (int  i = 0;i<arr.length;i++){
+            if(arr[i]>max){
+                max = arr[i];
+            }
+        }
+        System.out.println(max);
+    }
+
+
+    /**
+     *    找最值大及其所有最大值的下标
+     */
+    @Test
+    public void t3(){
+        int[] arr = {4,5,6,1,9,9,3};
+        int max = arr[0];
+        String index  = "0";
+        for (int i = 0; i < arr.length; i++) {
+         if(arr[i] > max){
+             max = arr[i];
+             index = i+"";
+         }else if (arr[i] == max){
+             index += "   "+i;
+         }
+        }
+        System.out.println(max);
+        System.out.println(index);
+    }
+
+    @Test
+    public void t4(){
+        int[] scores = {5,4,6,8,9,0,1,2,7,3};
+        int max = scores[0];
+        int min = scores[0];
+        int sum = 0;
+        for (int i = 0; i < scores.length; i++) {
+            if (scores[i] > max) {
+                max = scores[i];
+            }
+            if (scores[i] < min) {
+                min = scores[i];
+            }
+            sum += scores[i];
+        }
+        double avg = (sum - max -min)*1.0/(scores.length-2);
+        System.out.println("总分"+sum);
+        System.out.println("最高分"+max);
+        System.out.println("最低分"+min);
+        System.out.println("平均分"+avg);
+    }
+}

+ 237 - 0
src/main/java/com/sf/day03/T.java

@@ -0,0 +1,237 @@
+package com.sf.day03;
+import org.junit.jupiter.api.Test;
+import java.util.Arrays;
+
+/**
+ * day03   while
+ */
+public class T {
+    /**
+     * while循环的语法结构:
+     *  while(循环条件){    true   永真循环   死循环
+     *      //循环体
+     *  }
+     */
+    @Test
+    public void t1(){
+        //利用while循环计算1-100的和
+        int i  = 1;
+        int sum = 0;
+        while (i<=100){
+            sum += i;
+            i++;
+        }
+        System.out.println("总和  = "+sum);
+    }
+
+    /**
+     * do  while
+     * do{
+     *
+     * }while();
+     *
+     * do while  无论条件是否满足  都先执行一次
+     * while  先判断条件是否满足
+     */
+    @Test
+    public void t2(){
+        //用 do while循环写一下1-100的和
+        int  i = 1;
+        int sum = 0;
+        do {
+            sum += i;
+            i++;
+        }while (i <=100);
+        System.out.println(sum);
+    }
+
+    /**
+     * 9*9乘法表
+     */
+    @Test
+    public void t3(){
+        for (int i = 1;i<=9;i++){
+            for (int j = 1;j<=i;j++){
+                System.out.print(i+"*"+j+"="+i*j+"\t");
+            }
+            System.out.println();
+        }
+    }
+
+    /**
+     * //1—100之间的所有质数
+     * (质数是只能被1和他本身整除的数称为质数,1既不是质数也不是合数)
+     */
+    @Test
+    public void t4(){
+        for (int i = 2;i<=100;i++){
+            boolean flag = true;
+            for (int j = 2;j<i;j++){
+                if(i%j == 0){
+                    flag = false;
+                    break;
+                }
+            }
+            if(flag){
+                System.out.println(i);
+            }
+        }
+    }
+
+    /**
+     * 打印5行直角三角形
+     */
+    @Test
+    public void t5(){
+        for(int i = 1;i<=5;i++){
+            for (int j  = 1;j<=i;j++){
+                System.out.print("*");
+            }
+            System.out.println();
+        }
+    }
+
+    /**
+     * 将一天的时间打印到当前控制台上
+     */
+    @Test
+    public void t6(){
+        for(int hour = 0;hour<24;hour++){
+            for(int min = 0;min<60;min++){
+                for(int s = 0;s<60;s++){
+                    System.out.println(hour+"时"+min+"分"+s+"秒");
+                }
+            }
+        }
+    }
+
+    @Test
+    public void t7(){
+        /**
+         * 生成 1-100 之间的随机数,直到生成了 97 这个数,看看一共用了几次?
+         */
+        int count = 0;//记录次数
+        while (true){
+            int random = (int) (Math.random()*100)+1;
+            count++;
+            if(random == 97){
+                break;
+            }
+        }
+        System.out.println(count);
+    }
+
+    /**
+     * 方法:也称之为函数
+     * 方法的语法结构:权限修饰符   【其他修饰符】 方法的返回值类型   方法名(参数) throws异常{
+     *     //方法体
+     * }
+     *
+     * 权限修饰符;public
+     * 其他修饰符:static  静态的   静态方法
+     * 方法的返回值类型:有/无    int  string   对象   数组   集合 ....   无  void
+     * 方法名 :标识符    创建规则:见名知意
+     * 参数:入参  出参     语法结构    数据类型 参数名 , ....
+     * 入参:形参和实参  :   形参?  形式参数    get(int a);
+     *      实参:实际参数   我们在调用方法时传过来的参数
+     * //代码体:   有返回值  return   返回值
+     *              void    return;    可以  在java不建议
+     *             C: int main(){
+     *                 return 1;
+     *             }
+     * 注意点:方法不能单独存在  必须在类中   main函数    自定义的方法  并行
+     *          在同个类中  方法名不能相同
+     *
+     * static:静态方法 :   调用   在同类   不同类
+     *
+     * 重载方法:  在一个类中,方法名相同  参数列表不同(参数个数不同 参数数据类型不同 顺序))     (权限修饰符   返回值类型)X
+     */
+    @Test
+    public void t8(){
+        //  实例方法可以调用实例方法、静态方法
+    insert();
+    insert("admin");
+    update();
+    }
+
+    //主函数
+    public static void main(String[] args) {
+//        insert();
+        // 静态方法必须调用静态
+        update();
+        String[] studentsInfos = getStudentsInfos();
+        System.out.println(Arrays.toString(studentsInfos));
+        int studentIndex = 1;
+        String student= getStudentInfosByIndex(studentIndex);
+        System.out.println(student);
+        char aChar = getChar();
+        System.out.println(aChar);
+    }
+
+
+    //自定义个一个方法
+    public void insert(){
+        System.out.println("信息插入");
+    }
+
+    public void insert(String name){
+        System.out.println("信息插入");
+    }
+
+    public void insert(int age,String name){
+        System.out.println("信息插入");
+    }
+
+    public void insert(String name,int age){
+        System.out.println("信息插入");
+    }
+
+
+    public static void update(){
+        System.out.println("update");
+    }
+
+    public static String[] getStudentsInfos(){
+       String[] str = new String[]{"zs","ls","ww"};
+       return str;
+    }
+
+    public static String getStudentInfosByIndex(int index){
+        String[] str = new String[]{"zs","ls","ww"};
+        return str[index];
+    }
+
+    public int sum(int ... args){
+        int sum1 = 0;
+        for (int i = 1;i<args.length;i++){
+            sum1 += args[i];
+        }
+        return sum1;
+    }
+
+    /**
+     * //设计一个方法可以返回A-Z之间的随机字符。 [65 90]
+     * public char getChar() {
+     * }
+     */
+    public static char getChar() {
+        return (char) ((Math.random()*26)+65);
+     }
+
+    /**
+     * 计算n 的阶乘
+     * @return
+     */
+    public int jc(int n){
+        if(n  == 1){
+            return 1;
+        }
+        return n * jc(n-1);
+    }
+
+    @Test
+    public void t10(){
+        int jc = jc(8);
+        System.out.println(jc);
+    }
+}

+ 27 - 0
src/main/java/com/sf/day03/T2.java

@@ -0,0 +1,27 @@
+package com.sf.day03;
+
+import org.junit.jupiter.api.Test;
+
+/**
+ * T2类
+ */
+public class T2 {
+    public static void main(String[] args) {
+        //  不同类  静态方法调用   类名.方法名
+        //         实例方法   new对象   对象名.方法名
+        T.update();
+        T t = new T();
+        t.insert();
+        t.insert("admin");
+    }
+
+
+    @Test
+    public void t1(){
+        //实例方法    静态方法
+        T t = new T();
+        t.insert("ss");
+        t.insert();
+        T.update();
+    }
+}

BIN
target/classes/com/sf/day03/ArrayTest.class


BIN
target/classes/com/sf/day03/ArrayWorker.class


BIN
target/classes/com/sf/day03/T.class


BIN
target/classes/com/sf/day03/T2.class