Java正在尝试更改方法和构造函数中的变量值

Java正在尝试更改方法和构造函数中的变量值,java,Java,我编写的代码的工作原理如下: 有一个Superhero类,包含实例变量、两个构造函数(一个可能有错误的方法称为powerUp)和一个在两个对象实例之间发起斗争的方法 当我运行代码时,我从命令行获得以下输出: 独眼巨人10 巨像40 获胜者是:巨像 通电后:独眼巨人的力量为:50 测试独眼巨人的力量:50 获胜者是:巨像 在第一个“胜利者是巨人”之后,应该是独眼巨人作为胜利者,因为这个对象实例获得100的加电,这是40的加电,但是由于某些原因,这个规则没有得到应用,代码说巨人仍然是胜利者。。程序根

我编写的代码的工作原理如下: 有一个Superhero类,包含实例变量、两个构造函数(一个可能有错误的方法称为powerUp)和一个在两个对象实例之间发起斗争的方法

当我运行代码时,我从命令行获得以下输出:

独眼巨人10 巨像40 获胜者是:巨像 通电后:独眼巨人的力量为:50 测试独眼巨人的力量:50 获胜者是:巨像

在第一个“胜利者是巨人”之后,应该是独眼巨人作为胜利者,因为这个对象实例获得100的加电,这是40的加电,但是由于某些原因,这个规则没有得到应用,代码说巨人仍然是胜利者。。程序根据力量计算胜利者,最高者为胜利者,或者如果两者相等,则对手为胜利者

代码显示在下面

超级英雄职业:

public class Superhero{

//private instance variables declared below
//name of the super hero
private String name;
//name of the strength variable
private int strength;
//The variable for the powerUp method
private int powerUp;
//variable to store the power of the power up
private int storePower;



//Getter methods
public int getStrength(){
    return strength;
}

public String getName(){
    return name;
}










//constructor for if the player wanted to specify the name and the strength
public Superhero(String name, int strength){
    this.name = name;
    this.strength = strength;

    System.out.println(name + " " + strength);
}



//if the user doesn't enter the strength as it is optional
//this constructor below will run and set the default
public Superhero(String name){
    this.name = name;
    this.strength = 10;
    System.out.println(name + " " + strength );

}


/*
This method takes in an integer paremeter and based on that it does
some calculations.

There are four possible integer values you can pass in: 100, 75, 50 and 25.

If 100 has been passed in the storePower variable is set to 40 which would be
a power-up of 40, if 75 then power-up of 30, if 50 then a power-up of 20,
if 25 then a power-up of 10 and if the right ammount is not specified a power-up
of 0 will be assigned.

*/
public void powerUp(int powerUp){
    this.powerUp = powerUp;
    if(powerUp == 100){
        strength = strength + 40;
    }else if(powerUp == 75){
        strength = strength + 30;
    }else if(powerUp == 50){
        strength = strength + 20;
    }else if(powerUp == 25){
        strength = strength + 10;
    }else{
        strength = strength + 0;
    }
    this.strength = strength + storePower;
    System.out.println("After receiving a power-up: " + name +  " has a Strength of: " + strength);


}


//the method for fight
public Superhero fight(Superhero opponent) {
  if (this.getStrength() > opponent.getStrength()){
    return this;
  }else if(this.getStrength() == opponent.getStrength()){
    return opponent;
  }else{
    return opponent;
  }
}
}

战斗等级:

public class Fight{
public static void main(String[] args){

    // a. Create a Superhero named Cyclops
    Superhero Cyclops = new Superhero("Cyclops");

    //b. Create a Superhero named Colossus. Colossus has a strength of 40
    Superhero Colossus = new Superhero("Colossus", 40);

    //c. Make Cyclops fight Colossus. Print the winner to the terminal.
    Superhero winner = Cyclops.fight(Colossus);
    System.out.println("The winner is: " + winner.getName());

    //d. Give Cyclops a powerUp of 100
    Cyclops.powerUp(100);

    //e. Make Cyclops fight Colossus. Print the winner to the terminal.
    Superhero winner2 = Cyclops.fight(Colossus);
    System.out.println("Testing the Cyclops strength: " + Cyclops.getStrength());
    System.out.println("The winner is: " + winner.getName());


}
}

在调试过程中,我尝试了我所能想到的一切,我已更改了以下代码:

public void powerUp(int powerUp){
    this.powerUp = powerUp;
    if(powerUp == 100){
        strength = strength + 40;
    }else if(powerUp == 75){
        strength = strength + 30;
    }else if(powerUp == 50){
        strength = strength + 20;
    }else if(powerUp == 25){
        strength = strength + 10;
    }else{
        strength = strength + 0;
    }
    this.strength = strength;
    System.out.println("After receiving a power-up: " + name +  " has a Strength of: " + strength);


}

而且似乎没有什么变化,因为某些原因,Cyclops不会被设置为50。

您正在对winner调用getName(),它不应该是winner2吗

Superhero winner2 = Cyclops.fight(Colossus);
    System.out.println("Testing the Cyclops strength: " + Cyclops.getStrength());
    System.out.println("The winner is: " + winner.getName());

您似乎使用了两个变量来表示
powerUp
powerUp
storePower
,但是
storePower
从未更新,而且总是
0
我如何才能更新storePower?您真的需要吗?
Superhero winner2 = Cyclops.fight(Colossus);
    System.out.println("Testing the Cyclops strength: " + Cyclops.getStrength());
    System.out.println("The winner is: " + winner.getName());