gpt4 book ai didi

JAVA双输入错误(线程 "main"java.lang.NullPointerException中出现异常)

转载 作者:行者123 更新时间:2023-12-02 05:03:59 25 4
gpt4 key购买 nike

我的代码中出现Thread "main"java.lang.NullPointerException 异常错误。

这应该是玩家和怪物之间战斗的模拟。每两周我就会接到一项任务,用新功能对其进行升级。这周我必须制作另一个版本的怪物:这里它受到普通攻击的一半伤害和法术伤害的一半。(ChargeMonster.java 是另一个任务,但它有效)

问题出在 ResistantMonster.java 中,但我不知道它是什么,所以我将包含我的整个代码。

Exception in thread "main" java.lang.NullPointerException
at ResistantMonster.takeDamage(ResistantMonster.java:12)
at Character.attack(Character.java:48)
at Game.main(Game.java:161)

代码:

public class ResistantMonster extends Monster {

public ResistantMonster(int maxHp, int atk, double hitChance) {
super(maxHp, atk, hitChance);
}

public void takeDamage(int damage) {
if (input.equals("1")) { // exception occurs here
currentHp -= ((int) 0.5 * damage);
} else {
currentHp -= ((int) 1.5 * damage);
if (getCurrentHp() < 0) {
currentHp = 0;
}
}
}

public class Game {

public static void main(String[] args) {
Player player = new Player(150, 30, 60, 3, 0.7, 100, 15);
Monster[] monsters = new Monster[5];
for (int i = 0; i < 5; i++) {
monsters[i] = new ResistantMonster(100 + (int) (Math.random() * 101), 20 + (int)
}
Monster monster = monsters[(int) (Math.random() * 5)];

Scanner sc = new Scanner(System.in);
String input;

int turn = 0;

while (player.getCurrentHp() > 0 && monster.getCurrentHp() > 0) {
System.out.println("----------------------------------------------------------------");
System.out.println("Round " + turn);
System.out.println(player.toString());
System.out.println(monster.toString());
System.out.println("----------------------------------------------------------------");
System.out.println("Possible commands:");
System.out.println("1 -> Attack");
System.out.println("2 -> Item (" + player.getRemainingItemUses() + " remaining)");
System.out.println("3 -> Fire-Spell (AP 50)");
System.out.println("4 -> Water-Spell (AP 40)");
System.out.println("5 -> Thunder-Spell (AP 30)");
System.out.println("Which command??");

input = sc.nextLine();

if (input.equals("1")) {
turn++;
System.out.println("Player attacks!");
if ( player.attack(monster) != -1) { //!!!!errorline
System.out.println("Player hits!");
} else {
System.out.println("Player missed.");
}
} else if (input.equals("2")) {
turn++;
if ( player.heal(player.getHealingPower())) {
System.out.println("Player uses a healing potion.");
} else {
System.out.println("Player has no more potions.");
}
} else if (input.equals("3")) {
turn++;
if (player.spell1(monster)) {
System.out.println("Player uses a fire spell!");
} else {
System.out.println("Player has not enough ability points left.");
}
} else if (input.equals("4")) {
turn++;
if (player.spell2(monster)) {
System.out.println("Player uses a water spell!");
} else {
System.out.println("Player has not enough ability points left.");
}
} else if (input.equals("5")) {
turn++;
if (player.spell3(monster)) {
System.out.println("Player uses a thunder spell!");
} else {
System.out.println("Player has not enough ability points left.");
}
} else {
turn++;
System.out.println("Invalid Command");
}
player.regenerateAp();

if (monster.isDefeated()) {
break;
}

System.out.println("Monster attacks!");

int ret = monster.action(player);
if ( ret >= 0) {
System.out.println("Monster hits!");
} else if( ret == -1) {
System.out.println("Monster missed.");
} else {
System.out.println("Monster charges.");
}
}

sc.close();
System.out.println("----------------------------------------------------------------");
System.out.println("Game over in round " + turn);
System.out.println(player.toString());
System.out.println(monster.toString());
System.out.println("----------------------------------------------------------------");

if (player.isDefeated()) {
System.out.println("Player won!");
} else {
System.out.println("Player lost!");
}
}
}

public class Character {
protected int currentHp;
protected int atk;
protected double hitChance;

public Character(int maxHp, int atk, double hitChance) {
this.currentHp = maxHp;
this.atk = atk;
this.hitChance = hitChance;
}

public void takeDamage(int damage) {
currentHp -= damage;
if (getCurrentHp() < 0) {
currentHp = 0;
}
}

public boolean isDefeated() {
if (getCurrentHp() == 0) {
return true;
} else {
return false;
}
}

public int getAtk() {
return this.atk;
}

public int getCurrentHp() {
return this.currentHp;
}

public int attack(Character target) {
if (Math.random() <= hitChance) {
int damage = (int) (atk * (Math.random() + 1.0));
target.takeDamage(damage); //!!!!errorline
return damage;
} else {
return -1;
}
}

public String toString() {
return "HP " + currentHp + " - ATK " + atk + " - Hit Chance " + hitChance;
}
}

public class Monster extends Character {

public Monster(int maxHp, int atk, double hitChance) {
super(maxHp, atk, hitChance);
}

public int action(Character target) {
return attack(target);
}

public String toString() {
return "Monster: HP " + currentHp + " - ATK " + atk + " - Hit Chance " + hitChance;
}
}

public class ChargeMonster extends Monster {
private double chargeChance = 0.2;

private boolean charged = false;

public ChargeMonster(int maxHp, int atk, double hitChance) { // Konstruktor
super(maxHp, atk, hitChance);
}

public int action(Character target) {
if (!charged) {
double rdm = Math.random();
if ( rdm > chargeChance) {
return attack(target);
} else {
charged = true;
return -2;
}
} else {
return attack(target);
}
}

public int attack(Character target) {
if (!charged) {
if (Math.random() <= hitChance) {
int damage = (int) (atk * (Math.random() + 1.0));
target.takeDamage(damage);
return damage;
} else {
return -1;
}
} else {
if (Math.random() <= hitChance) {
int damage = (int) (atk * (Math.random() + 2.5));
target.takeDamage(damage);
charged = false;
return damage;
} else {
return -1;
}
}
}
}

PS:如果需要,我可以复制我的 Player.java

PS2:请对我的编码方式发表评论。任何改进的建议都会非常棒!提前致谢!

最佳答案

根据您的错误,您正在尝试访问空值。您在标记行上访问的唯一内容是输入。因此,您的错误告诉您 input 为空。

您在ResistantMonster类中的什么位置设置input?据我所知,通过查看您的代码,您在任何地方都没有它。

要解决此问题,请通过构造函数或方法takeDamage()

关于JAVA双输入错误(线程 "main"java.lang.NullPointerException中出现异常),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27973283/

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