gpt4 book ai didi

java - 重建一些代码以根据概率返回损坏或未命中(游戏)

转载 作者:行者123 更新时间:2023-12-01 19:47:02 30 4
gpt4 key购买 nike

你好,我正在尝试基于概率实现战斗和准确性。这是我的代码,但效果不太好。

public String setAttackedPartOfBodyPercent(String probability) {
return this.probability = probability; // from 0% - 100%
}

private int generateDamage() {
int miss = generateMiss(probability)+1);
damage = (new Random().nextInt(30 + getStrength()) + 1) * miss;
if (damage == 0)
gs.canvas.drawText("miss", (float) gs.chibi1.getX(), (float) gs.chibi1.getY(), pt.pBlack);
return damage;
}

public int generateMiss(int accuracy) {
if (accuracy < 100)
return new Random().nextInt((100 / accuracy)+1) > 0 ? 1 : 0;
else
return 1;
}

最佳答案

正确答案是

   public int generateMiss(int accuracy) {
if (accuracy == 100) return 1;
return (new Random().nextInt(101) < accuracy) ? 1 : 0;
}

关于java - 重建一些代码以根据概率返回损坏或未命中(游戏),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59115261/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com