因此,這裏是我的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;
}
}
}