好吧所以我正在创建一个与cpu战斗的程序,但是每次它首先输入一个小姐然后是一个致命的错误,我会破坏我的代码,这样你们就可以更容易地帮忙了。
进口:
var result = (from c in _db.rEmails
where c.Email.Contains(id)
select c.ALIAS_NAME)
.FirstOrDefault();
设置整数和确定命中类型的rng:
import javax.swing.JOptionPane;
import java.util.Random;
命中的类型和它们应分别做什么:
int life = 100; //Your life
int life2 = 100; //Enemy life
Random chance = new Random();
int rand = chance.nextInt(1)+100;
我愿意尝试任何事情,我迫切需要完成这些人,谢谢你的帮助。
答案 0 :(得分:2)
当您设置int rand = chance.nextInt(1)+100
时,您将获得0到1之间的随机整数,然后向其中添加100,因此rand
将为100或101.这会导致{{1}每次都要执行if / else语句的块。
我相信你想要的是//Fatality
。
此外,您在if语句中的比较不正确。您需要在每个if语句中切换chance.nextInt(100)+1
和<
。
>
编辑:正如@KonstantinosChalkias所指出的那样,使用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);
}
,您可以完全删除else if
部分逻辑。
&&
答案 1 :(得分:0)
刚刚加入安德鲁的回答,
似乎所有'命中'的if语句实际上都是不可能的
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);
}
上述情况永远不会成立,因为rand不能同时小于21且大于34,你确定你不是要代替OR语句吗?
如下所示:
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 >= 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);
}
哪个会更有意义
答案 2 :(得分:0)
正如已经指出的那样,您的if
语句重叠或逻辑错误。我想你想给出一些概率让我们说20%的人错过目标,15%的人“踩到你的脚趾”等等。实际上,您可以使用if-else语句
&&
if (rand <= 20 ){//Miss 20%
...
} else if (rand <= 35){//Wiff 15%
...
} else if (...){//for each of the following cases
...
} else {//Fatality
...
}