这是我的代码:
package dsboy08.BattleX;
import java.util.Scanner;
import java.util.Random;
/**
* Created by dsboy08 on 11/23/14.
*/
public class Start {
public String enemyName;
public int playerHP;
public int enemyHP;
public int playerMinDamageHit;
public int playerMaxDamageHit;
public int enemyMinDamageHit;
public int enemyMaxDamageHit;
public int playerHealMin;
public int playerHealMax;
public void setMod(String LocalModName, String LocalEnemyName, int LocalPlayerHP, int LocalEnemyHP, int LocalPlayerMinDamageHit, int LocalPlayerMaxDamageHit, int LocalEnemyMinDamageHit, int LocalEnemyMaxDamageHit, int LocalPlayerHealMin, int LocalPlayerHealMax){
playerHP = LocalPlayerHP;
enemyName = LocalEnemyName;
enemyHP = LocalEnemyHP;
playerMinDamageHit = LocalPlayerMinDamageHit;
playerMaxDamageHit = LocalPlayerMaxDamageHit;
enemyMinDamageHit = LocalEnemyMinDamageHit;
enemyMaxDamageHit = LocalEnemyMaxDamageHit;
playerHealMin = LocalPlayerHealMin;
playerHealMax = LocalPlayerHealMax;
print("Welcome to BattleX! Press 1 to start and 2 to exit.");
print("Mod: "+LocalModName+" loaded.");
Scanner menuItem = new Scanner(System.in);
String menuItem2 = menuItem.nextLine();
if(menuItem2.equals("1")) {
startGame();
}else {
if (menuItem2.equals("2")) {
System.exit(0);
} else {
System.exit(0);
}
}
}
public void print(String text){
System.out.println(text);
}
public void startGame(){
print("Current "+enemyName+" HP: "+playerHP);
print("Your current HP: "+enemyHP);
print("1: Attack");
print("2: Heal");
Scanner battleOption = new Scanner(System.in);
String battleOption2 = battleOption.nextLine();
if(battleOption2.equals("1")) {
attack();
}else {
if (battleOption2.equals("2")) {
heal();
}else{
startGame();
}
}
}
public void attack(){
Random humanAttack = new Random();
int attackDamage = humanAttack.nextInt((playerMinDamageHit - playerMaxDamageHit) + 1) + playerMinDamageHit;
print("Attacking for " + attackDamage + " damage.");
enemyHP = enemyHP - attackDamage;
int bearAttackDamage = humanAttack.nextInt((enemyMinDamageHit - enemyMaxDamageHit) + 1) + enemyMinDamageHit;
print("The "+enemyName+" attacked for "+bearAttackDamage+" damage.");
playerHP = playerHP - bearAttackDamage;
if(playerHP <= 0){
print("You failed! The "+enemyName+" killed you ;)");
System.exit(0);
}
if(enemyHP <= 0){
print("You defeated the "+enemyName+"! Good job!");
System.exit(0);
}
startGame();
}
public void heal(){
Random healAmount = new Random();
int randomHealAmount = healAmount.nextInt((playerHealMin - playerHealMin) + 1) + playerHealMin;
int bearAttackDamage = healAmount.nextInt((enemyMinDamageHit - enemyMaxDamageHit) + 1) + enemyMinDamageHit;
playerHP = playerHP + randomHealAmount;
if(playerHP + randomHealAmount >= 100){
playerHP = 100;
}
playerHP = playerHP - bearAttackDamage;
print("You have healed "+randomHealAmount+" health, but you have also lost "+bearAttackDamage+" health.");
if(playerHP <= 0){
print("You failed! The "+enemyName+" killed you ;)");
System.exit(0);
}
if(enemyHP <= 0){
print("You defeated the "+enemyName+"! Good job!");
System.exit(0);
}
startGame();
}
}
这是我的“主要”代码:
public static void main(String[] args){
new dsboy08.BattleX.Start().setMod("ExampleMod", "Death", 100, 50, 5, 10, 5, 15, 3, 10);
}
我从(似乎是一个静态上下文,但我似乎无法解决它)得到一个例外。
这是我的异常电话:
Exception in thread "main" java.lang.IllegalArgumentException: bound must be positive
at java.util.Random.nextInt(Random.java:388)
at dsboy08.BattleX.Start.attack(Start.java:67)
at dsboy08.BattleX.Start.startGame(Start.java:55)
at dsboy08.BattleX.Start.setMod(Start.java:33)
at dsboy08.BattleX.ModStart.main(ModStart.java:8)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
我尝试通过使方法不是静态来修复它,但它确实没有用。
答案 0 :(得分:2)
这里会发生什么:
humanAttack.nextInt((enemyMinDamageHit - enemyMaxDamageHit) + 1)
如果.nextInt(...)
方法的参数为负数?你会得到一个例外,就是这样。由于Max是>比敏,所以这很可能。您最好确保参数实际上是正面的。
我自己,我通过改变
交换它们humanAttack.nextInt((enemyMinDamageHit - enemyMaxDamageHit) + 1)
到
humanAttack.nextInt((enemyMaxDamageHit - enemyMinDamageHit) + 1)
最重要的是,您需要批判性地阅读所有异常消息,因为它们传递给您的信息不是乱码,而是经常(并且在您的情况下)精确地告诉您哪里出了问题以及在哪里。
您的异常消息告诉您:
Exception in thread "main" java.lang.IllegalArgumentException: bound must be positive
所以你知道一些用作方法或构造函数参数是否定的,但实际上应该是正面的。
at java.util.Random.nextInt(Random.java:388)
好的,它涉及与随机对象的nextInt(...)
电话有关。
at dsboy08.BattleX.Start.attack(Start.java:67)
,问题出在Start类的第67行,在攻击方法中。