Java初学 day three

1.数组

import java.lang.reflect.Array;
import java.sql.SQLOutput;

/**
 * @author: 2213040641陈子豪
 * @date: 2024/1/24 8:21
 * @description:
 */
public class ArrayDemo01 {
    public static void main(String[] args) {
  //      int[] arr = new int[5];
//        System.out.println(arr[0]);
//        System.out.println(arr[1]);
//        System.out.println(arr[2]);
//        System.out.println(arr[3]);
//        System.out.println(arr[4]);
//        System.out.println(arr[5]);   ArrayIndexOutOfBoundsException
        /*数组的遍历
        for(int i = 0;i < arr.length;i++){
            System.out.println(arr[i]);
        }*/
        //不赋值的情况下的默认值
        int[] arr1= new int[5];
        System.out.println(arr1[0]);
        boolean[] arr2 = new boolean[5];
        System.out.println(arr2[0]);
    }
}

2.双色球摇号

import java.util.Random;

/**
 * @author: 2213040641陈子豪
 * @date: 2024/1/24 8:45
 * @description:
 */
public class DoubleColorBall {
    public static void main(String[] args) {
        //声明一个int类型 长度为7的一维数组,动态
        int[] arr = new int[7];
        //创建一个随机数对象
        Random random = new Random();
//      开始摇号(向数组当中去添加值),需要先摇六红球,范围1-33
        for(int i = 0;i < arr.length-1;i++){
            //红球
            arr[i] = random.nextInt(33)+1;
            //去重
            for(int j = i - 1;j >= 0;j--){
                if(arr[i] == arr[j]){
                    //重复需要重新摇号
                    i--;
                    break;
                }
            }
        }
        //蓝球
        arr[arr.length-1] = random.nextInt(17)+1;
        //将数组中的双色球遍历
        System.out.println("本期中奖结果: ");
        for(int i = 0;i < arr.length;i++){
            System.out.print(arr[i]+" ");
        }
    }
}

3.构造方法

public class MethodDemo01 {
    public int add(int a,int b){
        return a+b;
    }

    public static void main(String[] args) {
        MethodDemo01 m = new MethodDemo01();
        int result = m.add(5,6);
        System.out.println("result = "+result);
    }
}

4.封装()入土

(1)

public class Person {

    //私有化成员变量
    private String name;
    private int age;

    public void show(){
        System.out.println("name =" + name + ",age = " + age);
    }
    //单元测试


    public Person() {
    }

    public Person(String name, int age) {
        setName(name);
        setAge(age);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if (age > 0 && age < 150){
            this.age = age;
        }else{
            this.age = 18;
            System.out.println("你输入的年龄有误!");
        }
    }

(2)

public class PersonTest {

    @Test
    public void test01(){
        /*Person person = new Person();
        person.show();
        person.setName("zhanghui");
        person.setAge(-1);
        person.show();*/
        Person person = new Person("陈子豪",-1);
        person.show();
    }
    @Test
    public void test02(){
        Phone phone = new Phone();
        phone.show();
    }
}

5.有参,无参构造方法

//当用户没有写任何形式的构造方法时,系统会自动的为程序提供一个无参构造方法
public class Phone {
    String brand;
    double price;
   /* public Phone(){
        System.out.println("我是无参构造方法。。。正在被调用");
}*/
    //如果用户自己编写了构造方法(有参或无参构造方法,系统不会在为我们提供任何形式的构造方法
    public Phone(String name){
        System.out.println("有参构造方法...");
    }
    public Phone(){ }
    //既不需要向方法体外传递数据内容,也不需要向方法体内传递数据内容

    public void show(){
        System.out.println("品牌:"+brand+" 价格: "+price);
    }
//是谁在调用main方法,JVM Java虚拟机
    public static void main(String[] args) {
        Phone p = new Phone();
        p.show();
        p.brand = "Nokie";
        p.price = 598.5;
        p.show();
    }
}

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值