攻击Dotcom小游戏简化版

7个空格,公司占3个空格,输入数值,和公司所占空格值一样,则显示hit,不一样则miss,三个都猜中后,gameover,显示分数。

import java.util.*;
class GameDotCom{
public static void main (String[] args){
    int js = 0;//有效攻击次数
    int jz = 0;//击中的次数

    Dotcom[] com = new Dotcom[3]; //一个公司的三个格子
    com[0] = new Dotcom();
    com[1] = new Dotcom();
    com[2] = new Dotcom();

while(true){//随机5之前的数字,包括5,作为第一个格子,之后两个格子依次叠加,可以随机出所有的情况。
    int r = (int)(Math.random()*10);
    if(r<5){
      com[0].x = r+1;
              com[1].x = r+2;
              com[2].x = r+3;
  //System.out.println(com[0].x+" "+com[1].x+" "+com[2].x);
      break;
        }
    }

System.out.println("Please Enter A Number Between 1-7");
while (true){

Scanner shuru = new Scanner(System.in);
Shot s = new Shot();
int k = shuru.nextInt();//取得玩家输入的攻击值

if(k < 8&&k > 0){//判断攻击值的大小
js = js + 1;//记录有效的攻击次数
if(k == com[0].x){
com[0].x = 0;jz = jz + 1;//击中后归0,并记录击中次数
System.out.println("Hit~~");
}
else if(k == com[1].x){
com[1].x = 0;jz = jz + 1;
System.out.println("Hit~~");
}
else if(k == com[2].x){
com[2].x = 0;jz = jz + 1;
System.out.println("Hit~~");
}
else {System.out.println("Miss~~");//没有击中

}//循环知道击中  可以改for语句
if (jz==3){
System.out.println("GameOver!! Marks "+(int)(300/js));
break;
    }
}else {System.out.println("Please Enter A Number Between 1-7");}//输入错误后重新输入
}

}
}


class Dotcom{
int x = 0;
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值