第二章3

package com.bdqn.dome2;


import java.util.Scanner;


import com.bdqn.dome1.Calculator;


public class Tvproducts {
public static void main(String[] args) {
Tvproducts1 c =new Tvproducts1();  //创建一个对象
Scanner intput=new Scanner(System.in);
System.out.println("请猜测高级电动车的价格");
int [] price = new int [3];  //创建数组接收商品 
       price[0] = 200; // 高级电动车  
       price[1] = 640; // 中级电动车
       price[2] = 700; // 低级电动车        
       System.out.println("商品编号:"+c.num +"\t商品名称:"+ c.tial() + "\t商品价格:"+price[c.num - 1]);  //打印随机出来的数值,无实际意义  
       System.out.print("请猜测" + "“" + c.tial() + "”" + "的价格:");  
       c.guess(c.num, c.num);  
       int a=0;  
       while (a != price[c.num-1] && a<3) {  //循环判断用户是否输入的价格正确,并且只给用户四次输入机会。  
           System.out.print("再猜一次吧:");  
           
           c.guess(c.num, c.num);  
           a++;  
           if (a>=3) {  
               System.out.println("4次内没有猜对,下次努力吧!");  
           }  
       }  
         
   }  
 

}

package com.bdqn.dome2;


public class Tvproducts1 {
 int num = 1+(int)(Math.random()*3); //随机产生1~3的数字;  
 public String tial(){
     String a3= "高级电动车";
     String a1= "中级电动车";
     String a2= "低级电动车";
   switch (num) {
case 1:
return a3;

case 2:
return a1;

default:
return a2;


   }
 }
public void guess(int num,int num2){  第一个参数为价格,第二个为随机产生的商品编号
switch (num2) {
case 1:
if (num >200) {  
           System.out.println("再小点!");  
       }else if (num == 200) {  
           System.out.println("猜对了!");  
       }else {  
           System.out.println("再大点!");  
       }  
       break;  
   case 2:  
       if (num >640) {  
           System.out.println("再小点!");  
       }else if (num == 640) {  
           System.out.println("猜对了!");  
       }else {  
           System.out.println("再大点!");  
       }  
       break;  
   default:  
       if (num >700) {  
           System.out.println("再小点!");  
       }else if (num == 700) {  
           System.out.println("猜对了!");  
       }else {  
           System.out.println("再大点!");  
       }  
       break;  
   }  
}  
}  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值