Java关于商品竞猜活动的案例(类和对象)--随机出现一个商品名,用户猜测它的价值,如果在规定次数内猜对,便可获得此商品。

##首先对题目进行分析:##

  • 准备一批用来竞猜的商品
  • 随机获取一件商品

首先我们先定义一个商品归属类 Good

public class Good {
    String name;  //商品名称
    double price;  //商品价格
 
    public Good(String name, double price) {  //有参构造
        this.name = name;
        this.price = price;
    }

    public Good() {
    }
}

##编写一个QuessGood类 其中的initial()方法预定义商品信息,根据产生的随机数字,选定一款竞猜的商品,编写guess()方法如果猜测正确,返回“猜对了!”;如果偏大,返回“再小些”;如果偏小,返回“再大些!4次没猜对返回 下次努力##

###其中会用到随机数的知识###

通过Math.random()方法可以生成[0,1)区间,可以为0,但是小于1。其他通常的使用方式如下   int index= (int) Math.floor(Math.random()*3); 就是求0-3之间的随机数 利用floor 转换int 进行向下取整数就可以得到0-2之间的随机数

-编写 initial()方法

public static Good initial(){
        //
        Good g1=new Good("洗衣机",5000.0);
        Good g2=new Good("吹风机",8000);
        Good g3=new Good("电动牙刷",500);

          //将对象存入数组
        Good [] gs={g1,g2,g3};
        int index= (int) Math.floor(Math.random()*3);
        return gs[index];
    }
  • 编写guess方法进行判定–主要要注意的就是当4次没猜对的时候就不在返回再猜一次直接返回下次努力吧
 public  static void guess(Good good){   //这里的参数是传入initial方法中随机出的商品(对象)
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入"+good.name+"的价格");
        int i=0;//计次数
        do{
            int x=sc.nextInt();
            if (good.price==x){
                System.out.println(good.name+"拿走吧");
                return;   //猜中后直接拿走
            }else if (good.price>x){
                System.out.println("再大点!");
            }else System.out.println("在小点!");
             if (i<3) System.out.println("再猜一次吧"); //前三次就输出在猜一次
             i++;

        }while (i<4);
        System.out.println("4次没有猜对下次努力吧");
    }```

 - 编写测试类模拟竞猜


public static void main(String[] args) {
//随机出一个商品
Good g=initial();
//猜价格
guess(g);

}

 - 整体代码如下  (测试类与QuessGood放在一起了)

public class QuessGood{

//随机出一个商品
public static Good initial(){
    //
    Good g1=new Good("洗衣机",5000.0);
    Good g2=new Good("吹风机",8000);
    Good g3=new Good("电动牙刷",500);

      //将对象存入数组
    Good [] gs={g1,g2,g3};
    int index= (int) Math.floor(Math.random()*3);
    return gs[index];
}
public  static void guess(Good good){
    Scanner sc = new Scanner(System.in);
    System.out.println("请输入"+good.name+"的价格");
    int i=0;//计次数
    do{
        int x=sc.nextInt();
        if (good.price==x){
            System.out.println(good.name+"拿走吧");
            return;
        }else if (good.price>x){
            System.out.println("再大点!");
        }else System.out.println("在小点!");
         if (i<3) System.out.println("再猜一次吧");
         i++;

    }while (i<4);
    System.out.println("4次没有猜对下次努力吧");
}
public static void main(String[] args) {
       //随机出一个商品
     Good g=initial();
     //猜价格
     guess(g);}}

运行效果:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值