因此,这里是我的Superhero
类:不明白为什么我得到空
public class Superhero {
public int strength;
public int powerUp;
public int defaultStrength = 10;
public String name;
public Superhero(String name) {
this.strength = 10;
System.out.println("The Superheroes available are :" + name);
}
public Superhero(String name, int strength) {
if (strength >= 0) {
this.strength = strength;
System.out.println("The Superheroes available are :" + name);
} else {
System.out.println("Error. Strength cannot be < 0");
}
}
public void setStrength(int strength) {
this.strength = strength;
}
public int getStrength() {
return strength;
}
public void powerUp(int powerUp) {
this.strength += powerUp;
}
}
这里是我Fight
类的问题,这里,当我运行它,我回来了赢家的结果是null
和我不不明白为什么这样做。
import java.io.*;
public class Fight {
public static void main (String args[]) {
Superhero gambit = new Superhero("Gambit");
Superhero groot = new Superhero("Groot", 79);
System.out.println("Gambit's strength is: " + gambit.strength);
System.out.println("Groot's strength is: " + groot.strength);
System.out.println("The winner of the fight is: " + fight(gambit, groot));
}
static String fight(Superhero a, Superhero b)
{
if (a.strength > b.strength)
{
return a.name;
} else
{
return b.name;
}
}
}