游戏健康计数器
Health counter for game
我的战列舰游戏中的健康计数器有问题。我需要的是,当调用该方法时,它会减少 1 点健康。所以假设生命值是 3,当玩家的飞船受到攻击时调用该方法。然后我需要它去 health-1
,并保持那个值。然后health=0
时,游戏结束
欢迎对本代码提出任何问题和改进,也欢迎批评。
更新:
public static void enemyShoot (int row, int col)
{
int shot1;
int shot2;
int health = 3;
Random enemyshot = new Random();
shot1 = enemyshot.nextInt(5)+1;
shot2 = enemyshot.nextInt(5)+1;
if (shot1 == row && shot2 == col)
{
System.out.println("You Have Been Hit");
health = Health(health);
}
}
public static int Health (int health)
{
if (health == 0){
System.out.println("You dead");
System.exit(0);
}
health = health-1;
return health;
}
当您将健康作为参数传递给 Health 方法时,它会制作一个副本以供此方法使用。所以,当你递减和分析它时,副本递减到 2(如果你最初传递 3),但原始变量仍然等于 3。所以,事实上,健康计数器永远不会递减。
叫做"passing argument by value",你可以看看这个问题:What's the difference between passing by reference vs. passing by value?
正确的做法是递减方法中的值,然后 return 递减结果。您还应该这样调用您的方法:
health = Health(health);
此外,arsendavtyan91 是对的,您从 health = 0 开始...您可能希望将实际值传递给方法 enemyShoot
您正在将健康值初始化为 0
int health = 0;
然后当你射中目标时你
Health(health);
减去 1 然后测试 0
health = health-1;
if (health == 0)
你的健康现在是-1所以它永远不会== 0
您应该将 health 设置为某个正值,或者将您的测试更改为
if (health <= 0)
如果 health
等于零,则不需要减去。
游戏将继续,因为您总是在 enemyShoot()
方法的 int health = 3;
行初始化 health
,就在调用 Health()
方法。
所以我建议您在 class 中声明 health
(就像一个全局变量)并通过将其传递给 a constructor
来初始化它,如下所示:
int health;
public ClassName(int health){//this is a constructor with the `health` argument
this.health = health;
}
public static void enemyShoot (int row, int col)
{
int shot1;
int shot2;
Random enemyshot = new Random();
shot1 = enemyshot.nextInt(5)+1;
shot2 = enemyshot.nextInt(5)+1;
if (shot1 == row && shot2 == col)
{
System.out.println("You Have Been Hit");
health = Health(health);
}
}
public static int Health (int health)
{
if (health == 0){
System.out.println("You dead");
System.exit(0);
}
health = health-1;
return health;
}
首先,您真的应该阅读更多关于面向对象编程的基础知识。我的回答是基于你提供的那一小段代码,所以我不确定是否已经有合适的实现...
不过,我想你想要的是创建一个 Battleship Object,它的成员变量是 initial health。您可以使用参数定义坐标、方向等,但我会将它们从这个示例中删除。成功创建此对象后,使用 enemyShoot 方法计算战列舰是否已被击中并减少健康状况。结果是这样的:
public class Battleship {
int m_health;
public BattleShip() {
m_health = 3;
}
public enemyShoot(int x, int y) {
// TODO: calculate if hit
if (hit == true) {
m_health--;
if (m_health == 0)
System.out.println("You dead");
System.exit(0);
}
}
}
因此,为了使用上面的代码,我将把它放在一个名为 Battleship.java 的文件中,并创建一个主要方法来初始化我希望使用的对象
public static void main(String ... args) {
Battleship bs = new Battleship(); // <-- Creates a new Battleship object
bs.enemyShoot(0,0); // <-- shoot the battleship
bs.enemyShoot(0,0);
bs.enemyShoot(0,0); // <--At this point battleship would be destroyed, if hit
}
您提供的 Health 方法的问题在于它采用原始数据类型 int 作为参数(最初设置为 0),每次调用 Health 时都会分配 -1。调用此方法不会影响 enemyShoot 方法中的健康值,因为这不是对象而是原始数据类型的内部方法变量。
希望这能帮助您继续完成作业。 :)
你已经接近你的健康方法,你想要的是这样的:
public static void hit() {
this.health--;
if (health <= 0) {
System.out.println("You died.");
System.exit(0);
}
}
Java 是一种传递引用,因此您需要更新刚被击中的对象的健康状况。在这种情况下,我假设您的 Health()
在该对象内部。
否则你从你的 health = 3;
开始,每次被击中它都会变成 health = 2;
但被击中的物体将始终保持 3
生命值。同样,在没有看到更多您的代码的情况下,我无法准确地说出执行此操作的最佳方法,因此我不得不假设一些事情。
需要注意的是,这将很快退出程序,您甚至不会看到 You died
消息。
我的战列舰游戏中的健康计数器有问题。我需要的是,当调用该方法时,它会减少 1 点健康。所以假设生命值是 3,当玩家的飞船受到攻击时调用该方法。然后我需要它去 health-1
,并保持那个值。然后health=0
时,游戏结束
欢迎对本代码提出任何问题和改进,也欢迎批评。
更新:
public static void enemyShoot (int row, int col)
{
int shot1;
int shot2;
int health = 3;
Random enemyshot = new Random();
shot1 = enemyshot.nextInt(5)+1;
shot2 = enemyshot.nextInt(5)+1;
if (shot1 == row && shot2 == col)
{
System.out.println("You Have Been Hit");
health = Health(health);
}
}
public static int Health (int health)
{
if (health == 0){
System.out.println("You dead");
System.exit(0);
}
health = health-1;
return health;
}
当您将健康作为参数传递给 Health 方法时,它会制作一个副本以供此方法使用。所以,当你递减和分析它时,副本递减到 2(如果你最初传递 3),但原始变量仍然等于 3。所以,事实上,健康计数器永远不会递减。
叫做"passing argument by value",你可以看看这个问题:What's the difference between passing by reference vs. passing by value?
正确的做法是递减方法中的值,然后 return 递减结果。您还应该这样调用您的方法:
health = Health(health);
此外,arsendavtyan91 是对的,您从 health = 0 开始...您可能希望将实际值传递给方法 enemyShoot
您正在将健康值初始化为 0
int health = 0;
然后当你射中目标时你
Health(health);
减去 1 然后测试 0
health = health-1;
if (health == 0)
你的健康现在是-1所以它永远不会== 0 您应该将 health 设置为某个正值,或者将您的测试更改为
if (health <= 0)
如果
health
等于零,则不需要减去。游戏将继续,因为您总是在
enemyShoot()
方法的int health = 3;
行初始化health
,就在调用Health()
方法。
所以我建议您在 class 中声明 health
(就像一个全局变量)并通过将其传递给 a constructor
来初始化它,如下所示:
int health;
public ClassName(int health){//this is a constructor with the `health` argument
this.health = health;
}
public static void enemyShoot (int row, int col)
{
int shot1;
int shot2;
Random enemyshot = new Random();
shot1 = enemyshot.nextInt(5)+1;
shot2 = enemyshot.nextInt(5)+1;
if (shot1 == row && shot2 == col)
{
System.out.println("You Have Been Hit");
health = Health(health);
}
}
public static int Health (int health)
{
if (health == 0){
System.out.println("You dead");
System.exit(0);
}
health = health-1;
return health;
}
首先,您真的应该阅读更多关于面向对象编程的基础知识。我的回答是基于你提供的那一小段代码,所以我不确定是否已经有合适的实现...
不过,我想你想要的是创建一个 Battleship Object,它的成员变量是 initial health。您可以使用参数定义坐标、方向等,但我会将它们从这个示例中删除。成功创建此对象后,使用 enemyShoot 方法计算战列舰是否已被击中并减少健康状况。结果是这样的:
public class Battleship {
int m_health;
public BattleShip() {
m_health = 3;
}
public enemyShoot(int x, int y) {
// TODO: calculate if hit
if (hit == true) {
m_health--;
if (m_health == 0)
System.out.println("You dead");
System.exit(0);
}
}
}
因此,为了使用上面的代码,我将把它放在一个名为 Battleship.java 的文件中,并创建一个主要方法来初始化我希望使用的对象
public static void main(String ... args) {
Battleship bs = new Battleship(); // <-- Creates a new Battleship object
bs.enemyShoot(0,0); // <-- shoot the battleship
bs.enemyShoot(0,0);
bs.enemyShoot(0,0); // <--At this point battleship would be destroyed, if hit
}
您提供的 Health 方法的问题在于它采用原始数据类型 int 作为参数(最初设置为 0),每次调用 Health 时都会分配 -1。调用此方法不会影响 enemyShoot 方法中的健康值,因为这不是对象而是原始数据类型的内部方法变量。
希望这能帮助您继续完成作业。 :)
你已经接近你的健康方法,你想要的是这样的:
public static void hit() {
this.health--;
if (health <= 0) {
System.out.println("You died.");
System.exit(0);
}
}
Java 是一种传递引用,因此您需要更新刚被击中的对象的健康状况。在这种情况下,我假设您的 Health()
在该对象内部。
否则你从你的 health = 3;
开始,每次被击中它都会变成 health = 2;
但被击中的物体将始终保持 3
生命值。同样,在没有看到更多您的代码的情况下,我无法准确地说出执行此操作的最佳方法,因此我不得不假设一些事情。
需要注意的是,这将很快退出程序,您甚至不会看到 You died
消息。