Reputation: 194
So I'm attempting to create a very simple program to practice some basic Java formatting skills. However, something about the "fight()" is driving my compiler crazy. Does anyone see why? Thanks in advance for any answers I receive, code below:
class Hero{
String name;
int Intelligence;
boolean parents;
public static fight(Hero1, Hero2){
if(Hero1.Intelligence>Hero2.Intelligence){
return(Hero1.name+" is the winner");
else
return(Hero2.name+" is the winner");
}
}
}
class HeroMain{
public static void main(String[] args){
Hero Superman = new Hero();
Superman.name = "Superman";
Superman.Intelligence = 7;
Superman.parents = false;
Hero Batman = new Hero();
Batman.name = "Batman";
Batman.Intelligence = 8;
Batman.parents = false;
public fight(Superman, Batman);
}
}
Upvotes: 0
Views: 166
Reputation: 2372
Here are a few corrections:
class Hero{
String name;
int Intelligence;
boolean parents;
public static String fight(Hero Hero1, Hero Hero2){ <-- specify type of parameter and return type
if(Hero1.Intelligence>Hero2.Intelligence) <-- you had a curly-brace problem
return(Hero1.name+" is the winner");
else
return(Hero2.name+" is the winner");
}
}
class HeroMain{
public static void main(String[] args){
Hero Superman = new Hero();
Superman.name = "Superman";
Superman.Intelligence = 7;
Superman.parents = false;
Hero Batman = new Hero();
Batman.name = "Batman";
Batman.Intelligence = 8;
Batman.parents = false;
String myStr = Hero.fight(Superman, Batman); <-- call a hero's fight method
System.out.println(myStr); // "Batman is the winner"
}
}
Upvotes: 1
Reputation: 371
as La-comadreja stated the issue is the fact your trying to return a string, yet you do not define that on your method header.
example
public static void fight() {}
does not return, it just does something
public static String fight(){}
returns a String
Upvotes: 3
Reputation: 5755
you need to write
public static String fight(Hero hero1, Hero hero2) {
You also need to call fight()
as follows:
Hero.fight(Superman, Batman);
Also, as a rule of thumb in Java, you should begin all your variables with a lowercase letter. That's just coding convention.
Upvotes: 5