**已关闭。**此问题需要debugging details。当前不接受答案。
编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
3天前关闭。
Improve this question
我正在做一个小的文本冒险,基于我在网上看到的教程,每次我输入1,2或3,它都会一直循环回到我的无效命令。这是假设,只有当用户输入这3个数字中的任何一个时,它才会启动。有什么想法吗?我错过了什么?
String input = in.nextLine();
if (input.equals(1)) {
int damageDealt = rand.nextInt(attackDamage);
int damageTaken = rand.nextInt(enemyAttackDamage);
enemyHealth -= damageDealt;
health -= damageTaken;
System.out.println ("\t> You strike the " + enemy + " for " + damageDealt + " damage. ");
System.out.println ("\t> You receive " + damageTaken + " in retaliation! ");
if(health < 1) {
System.out.println ("\t> You have taken too much damage, you are too weak to go on!");
break;
}
}
else if (input.equals(2)) {
if(numHealthPots > 0) {
health += healthPotionHealAmount;
numHealthPots--;
System.out.println ("\t You drink a health potion, healing yourself for " + healthPotionHealAmount +"." + "\n\t> You now have " + health + " HP. " + "\n\t> You have " + numHealthPots + " health potions left. \n");
}
else {
System.out.println("\t You have no health potions left! Defeat an enemy for a chance to get one. ");
}
}
else if (input.equals(3)) {
System.out.println("\tYou run away from the " + enemy + "!");
continue GAME;
}
else {
System.out.println("\tInvalid command.");
}
}
if(health <1 ) {
System.out.println("\tYou are too weak to go on.");
break;
}
我检查输入的值是否有任何错误,即数字后面的任何点,但什么也没有出现。
1条答案
按热度按时间sshcrbum1#
如果你想用同样的方法比较String和int,可以使用这个