Domanda

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.

È stato utile?

Soluzione

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
}

Altri suggerimenti

Two prblems:

Firstly, you are trying to encapsulate a method inside a method that is not legal. Move your hitPoints outside main method.

Secondly you are trying to return an integer value from your hitPoints method but that does not return anything as per its definition:

public static void hitPoints()

So either change your hitPoints method signature to return an int or remove the return statement from the method as mentioned here:

 return 0;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top