随机商品猜价格

时光毁灭记忆、已成空白 提交于 2019-12-01 07:13:37

import java.util.Scanner;
public class QuessMachine01 {
 int num;
 String name;
 int money;
 int choice;
 
 public void initial(){
  num = (int)(Math.random()*10/3+1);
  switch (num) {
  case 1:
   dian();   
   break;
  case 2:
   zxc();   
   break;
  case 3:
   dianshuihu();   
   break;
  
  }
 }
 
 public void dian(){
  name = "电动自行车" ;
     money = 1000;
 }
 
 public void zxc(){
  name = "自行车" ;
  money = 500;
 }
 
 public void dianshuihu(){
  name = "电水壶" ;
  money = 200;
 }
 
 public void guess(){
  Scanner in = new Scanner (System.in);
  for (int i = 0; i < 4; i++) {
   if (i!=0) {
    System.out.println("在猜一次吧!");
    choice = in.nextInt();
   }
   if (i<3) {
    
    if (money==choice) {
     System.out.println("恭喜你,猜对了");
     break;
    }else if (money>choice){
     System.out.println("在小点");
     
    }else {
     System.out.println("在大点");
     
    }
    
   }
   if (i==3) {
    System.out.println("4次内没有猜对,下次努力吧!");
   }
  }
 }
 public static void main(String[] args) {
  QuessMachine01 q = new QuessMachine01 ();
  Scanner in = new Scanner (System.in);
  q.initial();
  System.out.println("请猜测"+q.name+"的价格");
  q.choice = in.nextInt();
  q.guess();
 }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!