package javabook;
import java.util.Scanner;
import java.util.Random;
public class Fight {
public static void main(String[] args) {
Tank tank1,tank2;
tank1 = new Tank();
tank2 = new Tank();
while(true)
{
@SuppressWarnings(“resource”)
Scanner order = new Scanner(System.in);
System.out.println(“操控坦克,请下达命令:”);
System.out.println(“1为填充炮弹命令,2为调整坦克速度命令,3为开火命令”);
int O = order.nextInt();
switch(O)
{
case 1:
Scanner bullet = new Scanner(System.in);
System.out.println(“填充炮弹:”);
System.out.println(“请选择给第一台坦克(输入1)或者第二台坦克(输入2)填充炮弹:”);
int choice = bullet.nextInt();
if(choice==1)
{
int P1 = bullet.nextInt();
tank1.setBulletAmount(P1);
System.out.println(“第一台坦克的炮弹数量为”+tank1.getBulletAmount());
}
else if(choice==2)
{
int P2 = bullet.nextInt();
tank2.setBulletAmount(P2);
System.out.println("第二台坦克的炮弹数量为"+tank2.getBulletAmount());
}
else
{
break;
}
break;
case 2:
Scanner speed = new Scanner(System.in);
System.out.println("调整坦克速度:");
System.out.println("请选择给第一台坦克(输入1)或者第二台坦克(输入2)调整速度:");
int choice2 = speed.nextInt();
if(choice2==1)
{
System.out.println("请选择给坦克加速或降速:");
System.out.println("1为加速,2为降速,3为刹车操作");
int speed1 = speed.nextInt();
if(speed1==1)
{
double s1 = speed.nextDouble();
tank1.speedUP(s1);
System.out.println("第一台坦克此时的速度为"+tank1.getSpeed());
}
else if(speed1==2)
{
double s1 = speed.nextDouble();
tank1.speedDOWN(s1);
System.out.println("第一台坦克此时的速度为"+tank1.getSpeed());
}
else if(speed1==3)
{
tank1.speedZero();
System.out.println("第一台坦克此时的速度为"+tank1.speedZero());
System.out.println("刹车成功!!!");
}
}
else if(choice2==2)
{
double s2 = speed.nextDouble();
System.out.println("请选择给坦克加速或降速:");
System.out.println("1为加速,2为降速,3为刹车操作");
int speed2 = speed.nextInt();
if(speed2==1)
{
tank2.speedUP(s2);
System.out.println("第二台坦克此时的速度为"+tank2.getSpeed());
}
else if(speed2==2)
{
tank2.speedDOWN(s2);
System.out.println("第二台坦克此时的速度为"+tank2.getSpeed());
}
else if(speed2==3)
{
tank1.speedZero();
System.out.println("第一台坦克此时的速度为"+tank2.speedZero());
System.out.println("刹车成功!!!");
}
}
else
{
break;
}
break;
case 3:
@SuppressWarnings("unused")
Scanner fire = new Scanner(System.in);
@SuppressWarnings("unused")
Random random = new Random();
int position1=0,position2=0;
position2 = position1 = (int)(Math.random()*2+1);
System.out.println("请选择让第一台坦克(输入1)或者第二台坦克(输入2)开火:");
int choice3 = fire.nextInt();
if(choice3==1)
{
System.out.println("开火!!!");
tank1.Fire();
System.out.println("第一台坦克剩余炮弹数量为"+tank1.getBulletAmount());
if(tank1.getBulletAmount()>=1)
{
System.out.println("请输入要攻击的位置:");
int fightsite1 = fire.nextInt();
if(fightsite1==position2)
{
System.out.println("击中目标!!!");
}
else
{
System.out.println("没有击中!!!");
}
}
else
{
System.out.println("子弹为空!!!");
break;
}
}
else if(choice3==2)
{
System.out.println("开火!!!");
tank2.Fire();
System.out.println("第二台坦克剩余炮弹数量为"+tank2.getBulletAmount());
if(tank2.getBulletAmount()>=1)
{
System.out.println("请输入要攻击的位置:");
int fightsite2 = fire.nextInt();
if(fightsite2==position1)
{
System.out.println("击中目标!!!");
}
else
{
System.out.println("没有击中!!!");
}
}
else
{
System.out.println("子弹为空!!!");
break;
}
}
}
}
}
}
class Tank{
double speed=0;
int bulletAmount=0;
void speedUP(double s1)
{
speed+=s1;
}
void speedDOWN(double s1)
{
speed-=s1;
}
double speedZero()
{
speed=0.00;
return speed;
}
void setBulletAmount(int n)
{
bulletAmount = n;
}
int getBulletAmount()
{
return bulletAmount;
}
double getSpeed()
{
return speed;
}
void Fire()
{
if(bulletAmount>=1)
{
bulletAmount-=1;
System.out.println("发射成功!!!");
}
else
{
System.out.println("报告,没有子弹了!!!");
}
}
}