所以我仍然是初学者,但我设法得到这个代码,但它没有像我想要的那样工作,我的主要问题是,每次按1都会重置敌人,而不是保持相同的。如果有人能帮助我,我真的很感激。到目前为止,我只写了1做某事。
package Game;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Random r = new Random();
System.out.println("Welcome to Dragon Heart");
System.out.println("1. Start");
System.out.println("2. Quit");
int input = 0, enemyhealth = 75, enemyattack = 15, playerhealth = 100, playerattack, random;
boolean enemydead = true, playerdead = false;
input = in.nextInt();
if (input == 1) {
System.out.println("Game started!");
while (0 != 1) {
if (enemydead = true) {
enemyhealth = r.nextInt(50) + 51;
enemyattack = r.nextInt(15) + 6;
System.out.println("An enemy appears, it has " + enemyhealth + " health points and " + enemyattack + " attack points");
} else {
System.out.println("The enemy now has " + enemyhealth + "health points");
}
System.out.println("1. Attack");
System.out.println("2. Defend");
System.out.println("3. Run away");
System.out.println("4. Do nothing");
input = in.nextInt();
if (input == 1) {
playerattack = r.nextInt(5) + 21;
random = r.nextInt(2) + 1;
enemyhealth = enemyhealth - playerattack;
if (random == 1) {
playerhealth = playerhealth - enemyattack;
}
if (enemyhealth <= 0) {
enemydead = true;
System.out.println("The enemy has been killed");
} else {
enemydead = false;
}
}
}
} else if (input == 2) {
System.out.println("Game quit.");
}
}
}
答案 0 :(得分:4)
你的防守逻辑是可疑的,但你的问题在这里:
if(enemydead = true)
您每次都会enemydead
重新分配true
。
你真的想检查敌人是否已经死亡,这是通过以下方式完成的:
if(enemydead)
此外,您可以将while (0 != 1)
清理为while(true)
。但是,您需要在该循环中的某处包含break
语句,以便它不像现在那样是无限循环。
最后,使用lower-case package names而不是大写包名称是个好主意。