【问题标题】:Rng Kind broken?Rng 种类坏了?
【发布时间】:2015-12-22 10:19:14
【问题描述】:

好吧,伙计们,请多多包涵,我需要添加一个健康栏,但在我添加健康栏之前,我需要让它正常工作。

问题: 当我运行它并且它滚动时,它将落在一个数字上并选择一个命中,当它确定命中时,它会运行多种类型的命中,而不仅仅是一个。如果愿意,请尝试运行它。希望 cmets 有所帮助。

我尝试了什么: 我已经尝试很好地梳理所有内容,但经过大约 2 小时的调整后,我没有找到我在这里求助的解决方案....

进口: 导入 javax.swing.JOptionPane; 导入 java.util.Random;

声明值:

    int life = 100; //Your life
    int life2 = 100; //Enemy life
    Random chance = new Random();



 while (life >= 0 && life2 >= 0){ //loop
int hitchance = chance.nextInt(100)+1; 
System.out.println(hitchance); //Your roll
if (hitchance <= 20 ){//Miss
    JOptionPane.showMessageDialog(null, "You have missed your opponent like a fool.\nYour Opponent has "+life2+" health remaining.");

}
if (hitchance >= 21 && hitchance <= 34){//Wiff
    int Wiff = chance.nextInt(10)+1;
    life = life-Wiff;
    JOptionPane.showMessageDialog(null, "You have stubbed your toe. Idiot.\nYou have "+life+" health remaining." +Wiff);

}
if (hitchance >= 35 && hitchance <= 74){//Regular Hit
    int regHit = chance.nextInt(20)+1;
    life2 = life2-regHit;
    JOptionPane.showMessageDialog(null, "You have hit your opponent for "+regHit+" damage!\nThey have "+life2+" health remaining.");

}
if (hitchance >= 75 && hitchance <= 90){//CritHit
    int critHit = chance.nextInt(40)+1;
    life2 = life2-critHit;
    JOptionPane.showMessageDialog(null, "You have dealt critical damage! "+critHit+"\nThey have "+life2+" health reamining.");

}
if (hitchance >= 91 && hitchance <= 100) {//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 hitchance2 = chance.nextInt(100)+1;
System.out.println(hitchance2); //Enemy roll
if (hitchance2 <= 20 ){//Miss
    JOptionPane.showMessageDialog(null, "Your opponent has missed you.\nYou have "+life+" health remaining.");

}
if (hitchance2 >= 21 && hitchance <= 34){//Wiff
    int Wiff = chance.nextInt(10)+1;
    life2 = life2-Wiff;
    JOptionPane.showMessageDialog(null, "Your enemy has stubbed his toe. Idiot.\nThey have "+life2+" health remaining." +Wiff+" Damage dealt.");

}
if (hitchance2 >= 35 && hitchance <= 74){//Regular Hit
    int regHit = chance.nextInt(20)+1;
    life = life-regHit;
    JOptionPane.showMessageDialog(null, "Your opponent has hit you for "+regHit+" damage!\nYou have "+life+" health remaining.");

}
if (hitchance2 >= 75 && hitchance <= 90){//CritHit
    int critHit = chance.nextInt(40)+1;
    life = life-critHit;
    JOptionPane.showMessageDialog(null, "Your opponent has dealt critical damage! "+critHit+"\nYou have "+life+" health reamining.");

}
if (hitchance2 >= 91 && hitchance2 <= 100) {//Fatality.      
    JOptionPane.showMessageDialog(null, "Fatality!\nYour opponent stabbed you in the foot,\ndrug their knife through"
            + "your belly,\nand impaled your head on his knife!");
    System.exit(0);

}
}
}
}

【问题讨论】:

  • 我觉得问题出在逻辑上.....

标签: java random


【解决方案1】:

代码中有几个看起来不正确的条件,例如

hitchance2 >= 21 && hitchance <= 34

这解释了为什么您可能会有随机点击。

【讨论】:

  • 谢谢伙计,这是第二份副本,其中的修订略少,例如有一些语法错误的文本错误
  • 实际上现在我改变了它运行良好的那些,我怎样才能将 JProgressBar 与 hp 结合起来?还是您认为不同的进度条会更好?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-13
  • 2020-04-25
  • 1970-01-01
  • 2014-02-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多