初学JAVA之简单模拟拳皇

xiaoxiao2021-02-28  33

1.设定双方角色的攻击力为5-15 

模拟攻击力时则需要用到 随机数取值:

Math.random( ) //初始取值范围为[0,1) 要将它的值确定到 5-15 则可用如下公式:

(int)(Math.random( )* X)%(b-a+1)+a // b>a  而X可以是随机的数值 X用来取消小数点 因此设定一个5-15的范围的数如下:

 (int)(Math.random( )* 10000)%(15-5+1)+5; 

2. 设定初始双方的血量为100  设定一个简短循环

代码如下:

public class 拳皇 { public static void main(String[] args) { int hp1=100;int hp2=100;//双方的Hp值 int attack1=0,attack2=0;//双方的攻击力 while(hp1>0&&hp2>=0) { attack1=(int)(Math.random()*10000)+5; attack2=(int)(Math.random()*10000)+5; hp2-=attack1;//玩家攻击电脑掉血 hp1-=attack2;//电脑攻击玩家掉血 } System.out.println("KO!"); if(hp1>0) { System.out.print("玩家获胜"); } else if(hp2>0) { System.out.println("电脑获胜"); } } }
转载请注明原文地址: https://www.6miu.com/read-2612602.html

最新回复(0)