第十一章课后作业 商品竞猜
来源:互联网 发布:linux tomcat无法启动 编辑:程序博客网 时间:2024/05/10 00:50
package ch11;import java.util.Random;import java.util.Scanner;public class QuessMachine{private String name;// 名字private int price;// 价格//数据类型 [] 数组名=new 数据类型[1];private QuessMachine [] machines;// 竞猜商品数组// 随机数对象privateRandom r=new Random();// 初始化方法private void initial(){// 数组下标最大值 是数组的长度-1machines=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;}// 猜的方法public void guess(){initial();// 初始化Scanner input = new Scanner(System.in);QuessMachine qm=machines[r.nextInt(5)];// System.out.println("请猜猜"+qm.name+"的价格");for (int i = 1; i <= 100; i++){int p=input.nextInt()/100;if(p<qm.price/100){System.out.println("小了, 再猜一次");}else if (p>qm.price/100) {System.out.println("大了, 在猜一猜");}else {System.out.println("恭喜你猜对了");break;}if(i==100){System.out.println("四次都没有猜对");}}}}
1 0
- 第十一章课后作业 商品竞猜
- 第十一堂课后作业
- 第十一堂课后作业
- 第十一堂课后作业
- 第十一堂课后作业
- 第十一堂课后作业
- 第十一堂课后作业
- 第十一堂课后作业
- 第十一堂课后作业
- 第十一堂课后作业
- 第十一堂课后作业
- 第十一堂课后作业
- 第十一堂课后作业
- 第十一堂课后作业
- 第十一堂课后作业
- 第十一堂课后作业
- 商品竞猜
- 第十一章 类的无参方法 课后作业
- 【android】一些不错的BLOG
- 虚函数与虚继承
- Linux Crontab 定时任务 命令详解
- vbox下安装的win7无法识别u盘的解决方法
- 如何让asp.net执行命令,暂停几秒,再执行下一行命令?(已解决)
- 第十一章课后作业 商品竞猜
- 用Openswan组建Linux IPSec ---第一部分
- linux 上安装jdk
- php格式化数字:位数不足前面加0补足
- java程序实现对文件的压缩和解压
- Entity Framework 教程
- 人机猜拳
- Zigbee解析: NWK层作用
- 使用bat批处理建立虚拟wifi