java第三周学习总结

第一天:

编程实现打印水仙花数:

public class GetNum {

       public static void main(String[] args) {

              //遍历所有的三位数

              for (int i = 100; i < 1000; i++) {

                     //调用自定义方法判断是不是水仙花数

                     if(isAim(i)) {

                            //如果是水仙花数,就打印

                            System.out.println(i);

                     }

              }

       }

 

       //判断水仙花数的方法

       public static boolean isAim(int a) {

              int x = a/100;

              int y = a/10%10;

              int z = a%10;

              if(a == x*x*x+y*y*y+z*z*z) {

                     return true;

              }

              return false;

       }

}

第二天:

编程字符排序输出:

import java.util.Arrays;

 

public class Subject4 {

       public static void main(String[] args) {

              char[]character={'a','c','u','b','e','p','f','z'};

              System.out.print("原字符序列:");

              for(int i=0;i<character.length;i++){

                     System.out.print(character[i]+" ");

              }

             

              System.out.print("\n升序排序后:");

              Arrays.sort(character);

              for(int i=0;i<character.length;i++){

                     System.out.print(character[i]+" ");

              }

             

              System.out.print("\n降序排序后:");

              for(int i=0 ;i<character.length;i++){

                     for(int j=character.length-1;j>=i+1;j--){

                            if(character[j]>character[j-1]){

                                   int temp=character[j-1];

                                   character[j-1]=character[j];

                                   character[j]=(char) temp;

                            }

                     }

              }

              for(int i=0;i<character.length;i++){

                     System.out.print(character[i]+" ");

              }

       }

}

第三天:

编程自定义数组:

import java.util.Scanner;

 

public class BubbleTest {

 

       public static void main(String[] args) {

              Bubble test = new Bubble();

              int[] ron = new int[10];

              for (int i = 0; i < ron.length; i++) {

                     ron[i] = (int) (Math.random() * 100 + 1);

              }

              for (int i : ron) {

                     System.out.print(i + " ");

              }

              Scanner input = new Scanner(System.in);

              System.out.println(" ");

              for (;;) {

                     System.out.println("请问您是升序还是降序?(up/down)");

                     int[] arr = ron;

                     test.sort(arr, input.next());

                     for (int i : arr) {

                            System.out.print(i + " ");

                     }

                     System.out.println(" ");

                     System.out.println("请问是否继续(Y/N)");

                     String s = input.next();

                     if (s.equals("Y")) {

 

                     } else {

                            System.out.println("谢谢使用!");

                            break;

                           

                     }

              }

       }

}

 

class Bubble {

       public int[] sort(int[] arr, String choose) {

              if (choose.equals("up")) {

                     for (int i = 0; i < arr.length - 1; i++) {

                            for (int j = 0; j < arr.length - i - 1; j++) {

                                   if (arr[j] > arr[j + 1]) {

                                          int temp = arr[j];

                                          arr[j] = arr[j + 1];

                                          arr[j + 1] = temp;

                                   }

                            }

                     }

                     return arr;

              } else {

                     for (int i = 0; i < arr.length - 1; i++) {

                            for (int j = 0; j < arr.length - i - 1; j++) {

                                   if (arr[j] < arr[j + 1]) {

                                          int temp = arr[j];

                                          arr[j] = arr[j + 1];

                                          arr[j + 1] = temp;

                                   }

                            }

                     }

                     return arr;

              }

       }

}

第四天:

构造方法和重造:

public class WuMingFen {

    private String name;

    private  int quantity;

    private boolean likeSoup;

 

    //构造方法1:含三个参数,为三个属性赋值。

    public WuMingFen(String name, int quantity, boolean likeSoup) {

        this.name = name;

        this.quantity = quantity;

        this.likeSoup = likeSoup;

    }

    //构造方法2:含两个参数,为前两个属性赋值

    public WuMingFen(String name, int quantity) {

        this.name = name;

        this.quantity = quantity;

    }

    //构造方法3:不含参数的构造方法,完成属性的初始化

    public WuMingFen() {

        name=" ";

        quantity=2;

        likeSoup=true;

    }

    //

    public String check(){

        return name+quantity+likeSoup;

    }

 

    public static void main(String[] args) {

        WuMingFen wuMingFen1=new WuMingFen(" ",1,true);

        WuMingFen wuMingFen2=new WuMingFen(" ",2);

        WuMingFen wuMingFen3=new WuMingFen();

        wuMingFen1.check();

        wuMingFen2.check();

        wuMingFen3.check();

        System.out.println(wuMingFen1.check());

        System.out.println(wuMingFen2.check());

        System.out.println(wuMingFen3.check());

    }

 

}

第五天:

This的用法:

class Student {

 

public Student() {

 

System.out.println("无参的构造方法");

 

}

 

public Student(String name) {

 

this();

 

System.out.println("一个参数的构造方法");

 

}

 

public Student(String name, int age) {

 

this(name);

 

System.out.println("两个参数的构造方法");

 

}

 

}

posted @   Espen  阅读(78)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
点击右上角即可分享
微信分享提示