数组作业2

public class phonesj {
    //品牌
    String brand;
    //库存数量
     int num;
    //单价
    double price;

    public void speak() {
        System.out.println("品牌:" + brand);
        System.out.println("库存数量:" + num);
        System.out.println("单价:" + price);
    }

    @Override
    public String toString() {
        return "phonesj{" +
                "brand='" + brand + '\'' +
                ", num=" + num +
                ", price=" + price +
                '}';
    }
}
import java.util.Arrays;
import java.util.Scanner;

public class phonesjkc {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("1.查看\t2.退出\t");

        do {
            String sj = input.next();
            switch (sj) {
                case "1":
                    phonesj p1 = new phonesj();
                    phonesj p2 = new phonesj();
                    phonesj p3 = new phonesj();
                    //赋值p1
                    p1.brand = "苹果";
                    p1.num = 6000;
                    p1.price = 60;

                    p2.brand = "梨";
                    p2.num = 5000;
                    p2.price = 50;

                    p3.brand = "香蕉";
                    p3.num = 5600;
                    p3.price = 56;

                    phonesj[] arr = new phonesj[3];
                    arr[0] = p1;
                    arr[1] = p2;
                    arr[2] = p3;

//                    System.out.println(Arrays.toString(arr));
                    for (int i = 0; i < arr.length; i++) {
                        //获取数组中的对象
                        System.out.println(arr[i]);
                    }
                    break;
                case "2":
                    System.out.println("退出");
                    System.exit(-1);
                    break;
            }
        } while (true);
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值