直接上代码
class="java">
package test;
import java.util.Random;
public class Test{
private static int rate = getRandom();
//如果没有获得装备,则下次获得装备的几率提升20%
//如果获得装备,则下次获得装备的几率降低20%
//你也可用不同的概率来分别表示获得装备和没有获得装备的概率
private static final int increadRate = 20;
//核心逻辑
private static void core() {
System.out.println("开始打Boss...");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Boss打掉,摸尸体开装备啦!!!");
boolean flag = hasEquipment(rate);
System.out.println("初始概率"+rate);
if(flag){
System.out.println("呵呵,我获得装备了");
//如果本次获得装备,则下次获得装备的概率-20%
rate = rate-increadRate;
if(rate<0){
rate = getRandom();
}
System.out.println("获得装备后的概率"+rate);
}else{
System.out.println("呜...本次没获得装备,不过下次获得装备的概率会提升"+increadRate+"%");
//如果本次获得装备,则下次获得装备的概率+20%
rate = rate+increadRate;
if(rate>100){
rate = getRandom();
}
System.out.println("没有获得装备后的概率"+rate);
}
}
//得到0-100间的随机数(包括0和100,即[0,100])
private static int getRandom(){
final int min = 0;
final int max = 100;
Random random = new Random();
return random.nextInt(max-min+1)+min;
}
/**
* 判断是否获得装备了
* @return true 获得装备,false 没有获得装备
*/
private static boolean hasEquipment(int randomNumber){
int random = getRandom();
if(random!=0&&(random>=0&&random<=randomNumber)){
return true;
}else{
return false;
}
}
//主程序入口
public static void main(String[] args) {
for(int i=0;i<10;i++){
core();
}
}
}