This is my assignment.
public class Fighter {
public static void main(String[] args){
String name;
int hitPoints;
int defenseLevel;
int attackSpeed;
Scanner input=new Scanner(System.in);
public static void hitPoints(){
int hitPoints;
do{
Scanner input=new Scanner(System.in);
System.out.println("What is the hit points for the fighter");
hitPoints=input.nextInt();
return hitPoints;
}while (hitPoints<=50);
return 0;
}
}
I'm pretty sure that looping it is completely wrong. I get an error Syntax error on token "void", @ expected"
.
I also tried it with different types such as int
and double
. No dice.
The assignment says to only use one void method:
Write two methods inside of Fighter Class;
public void input() public boolean attack(Fighter opponent)
However, I couldn't figure out how to do it with one so I was going to use 4 or 5.
First things first. You need to have hitPoints()
method outside the main. You can't nest it inside the main()
method.
And also, the return type of your hitPoints()
is void, you have return statements in the method. Change the return type to int
, so that you can return an int
value from this method to the calling method.
public static int hitPoints(){
Also, since its a do-while
loop(exit check loop), you don't need the default return. Instead initialize your hitPoints
to 0
by default.
public static int hitPoints() { // return type is int, to return an int value
int hitPoints = 0; // default value
do {
Scanner input = new Scanner(System.in);
System.out.println("What is the hit points for the fighter");
hitPoints = input.nextInt();
return hitPoints;
} while (hitPoints <= 50);
// return 0; // not required, as its a do-while loop above
}