gpt4 book ai didi

javascript - RNG 游戏正在制作中,需要帮助确定命中类型

转载 作者:行者123 更新时间:2023-12-02 04:14:00 26 4
gpt4 key购买 nike

好吧,我正在编写一个与 cpu 对抗的程序,但每次它都会先失败然后致命,我会分解我的代码,以便让你们更容易提供帮助。

进口:

import javax.swing.JOptionPane;
import java.util.Random;

设置整数和 rng 来确定命中类型:

int life = 100; //Your life
int life2 = 100; //Enemy life
Random chance = new Random();
int rand = chance.nextInt(1)+100;

点击的类型以及它们分别应该做什么:

while (life >= 0 && life2 >= 0){        
if (rand >= 20 ){//Miss
JOptionPane.showMessageDialog(null, "You have missed your opponent like a fool.\nYour Opponent has "+life2+" remaining.");
}
if (rand <= 21 && rand >= 34){//Wiff
int Wiff = chance.nextInt(10)+1;
life = life-Wiff;
JOptionPane.showMessageDialog(null, "You have stubbed your toe. Idiot.\nYou have "+life+" remaining."+Wiff);
}
if (rand <= 35 && rand >= 74){//Regular Hit
int regHit = chance.nextInt(20)+1;
life2 = life2-regHit;
JOptionPane.showMessageDialog(null, "You have hit your opponent!"+regHit);
}
if (rand <= 74 && rand >= 90){//CritHit
int critHit = chance.nextInt(40)+1;
life2 = life2-critHit;
JOptionPane.showMessageDialog(null, "You have dealt critical damage!"+critHit);
}
else {//Fatality.
JOptionPane.showMessageDialog(null, "Fatality!\nYou stabbed your opponent in the foot,\ndrug your knife throught"
+ "his belly,\nand impaled his head on your knife!");
System.exit(0);
}
}

我愿意尝试任何事情,我迫切需要完成这些人,感谢所有帮助。

最佳答案

当您设置int rand = chance.nextInt(1)+100时,您将获得一个 0 到 1 之间的随机整数,然后向其添加 100,因此 rand将为 100 或 101。这会导致 //Fatality每次都要执行的 if/else 语句 block 。

我相信你想要的是chance.nextInt(100)+1 .

此外,您的 if 语句中的比较是不正确的。您需要切换<>在每个 if 语句中。

if (rand <= 20 ){//Miss
JOptionPane.showMessageDialog(null, "You have missed your opponent like a fool.\nYour Opponent has "+life2+" remaining.");
}
else if (rand >= 21 && rand <= 34){//Wiff
int Wiff = chance.nextInt(10)+1;
life = life-Wiff;
JOptionPane.showMessageDialog(null, "You have stubbed your toe. Idiot.\nYou have "+life+" remaining."+Wiff);
}
else if (rand >= 35 && rand <= 74){//Regular Hit
int regHit = chance.nextInt(20)+1;
life2 = life2-regHit;
JOptionPane.showMessageDialog(null, "You have hit your opponent!"+regHit);
}
else if (rand >= 75 && rand <= 90){//CritHit
int critHit = chance.nextInt(40)+1;
life2 = life2-critHit;
JOptionPane.showMessageDialog(null, "You have dealt critical damage!"+critHit);
}
else {//Fatality.
JOptionPane.showMessageDialog(null, "Fatality!\nYou stabbed your opponent in the foot,\ndrug your knife throught"
+ "his belly,\nand impaled his head on your knife!");
System.exit(0);
}

编辑: 正如 @KonstantinosChalkias 所指出的,使用 else if ,您可以删除 &&完全是逻辑的一部分。

if (rand <= 20 ){//Miss
JOptionPane.showMessageDialog(null, "You have missed your opponent like a fool.\nYour Opponent has "+life2+" remaining.");
}
else if (rand <= 34){//Wiff
int Wiff = chance.nextInt(10)+1;
life = life-Wiff;
JOptionPane.showMessageDialog(null, "You have stubbed your toe. Idiot.\nYou have "+life+" remaining."+Wiff);
}
else if (rand <= 74){//Regular Hit
int regHit = chance.nextInt(20)+1;
life2 = life2-regHit;
JOptionPane.showMessageDialog(null, "You have hit your opponent!"+regHit);
}
else if (rand <= 90){//CritHit
int critHit = chance.nextInt(40)+1;
life2 = life2-critHit;
JOptionPane.showMessageDialog(null, "You have dealt critical damage!"+critHit);
}
else {//Fatality.
JOptionPane.showMessageDialog(null, "Fatality!\nYou stabbed your opponent in the foot,\ndrug your knife throught"
+ "his belly,\nand impaled his head on your knife!");
System.exit(0);
}

关于javascript - RNG 游戏正在制作中,需要帮助确定命中类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33509873/

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