十一章 作业

本文介绍了一个使用Java实现的竞猜商品初始化过程。通过创建一个包含多种商品的数组,并为每种商品设置名称和随机价格,展示了如何使用随机数生成器来设定商品的价格范围。涉及数组操作、对象实例化及随机数生成。
public class QuessMachine  
{  
    private String name;// 名字  
    private int price;// 价格  
      
    //数据类型  []  数组名=new 数据类型[1];  
    private QuessMachine [] machines;// 竞猜商品数组  
    // 随机数对象  
    private Random r=new Random();  
    // 初始化方法  
    private void initial()  
    {  
        // 数组下标最大值 是数组的长度-1  
        machines=new QuessMachine[5];// 实例化数组  
//      machines[0]  数组中的一个元素.属性  
        machines[0]=new QuessMachine();// 给数组中的元素实例化  
        machines[0].name="30寸液晶电视";// 设置商品名  
        machines[0].price=r.nextInt(1001)+1000;// 随机数范围,设置价格  
          
        machines[1]=new QuessMachine();  
        machines[1].name="samsung 笔记本电脑";  
        machines[1].price=r.nextInt(3001)+2000;  
          
        machines[2]=new QuessMachine();  
        machines[2].name="华为手机荣耀";  
        machines[2].price=r.nextInt(2001)+500;  
          
        machines[3]=new QuessMachine();  
        machines[3].name="挨骂电动车";  
        machines[3].price=r.nextInt(2001)+500;  
          
        machines[4]=new QuessMachine();  
        machines[4].name="大运摩托车";  
        machines[4].price=r.nextInt(2001)+4000;  
          
    }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值