Java - Cannot find symbol constructor

前端 未结 5 474
我在风中等你
我在风中等你 2021-01-15 03:23

I\'m completely new to Java, so I\'m sorry if my question is dumb. Im working on this assignment, and I\'ve been reading about main methods for hours now, but I just cant fi

相关标签:
5条回答
  • 2021-01-15 03:57

    Clearly you are using 0-arg constructor, when you haven't got one: -

    Player player = new Player();
    

    Note that, when you provide a parameterized constructor in your class, the compiler will not add default constructor. You would have to add one 0-arg constructor manually, if you are using it.

    So, either you can add one 0-arg constructor as such: -

    public Player() {
        this.nick = "";
        this.type = "";
        this.health = -1;
    }
    

    or, use the parameterized constructor to create the object.

    0 讨论(0)
  • 2021-01-15 03:59

    Player has no no-arg constructor, you could use:

    Player player = new Player("My Nickname", "Player Type");
    

    If you wish to prompt the user for the Player arguments, you can read like so:

    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter Player Name:");
    String nickName = scanner.nextLine();
    System.out.print("Enter Player Type:");
    String playerType = scanner.nextLine();
    Player player = new Player(nickName, playerType);
    
    0 讨论(0)
  • 2021-01-15 04:01

    A default constructor is created by java when a construtor is missing, this construtor simply calls the super class. When you defined an explicit constructor java wont create one. So you can either define one default constructor in your class e.g.

    public Player() 
    {   nick = "abc";
        type = "type";
        health = 100;
        System.out.println("Welcome " + nick +" the " + type + ". I hope you are ready for an adventure!");
    }
    

    or modify the code to call the constructor u defined.

     Player player = new Player("nick","type");
    
    0 讨论(0)
  • 2021-01-15 04:09

    What you tried to do in your main()-method was to create a new Player object. But the problem is that you had to use the constructor you implemented (Player(String, String)) but you used a constructor without any parameters (Player()).

    You should either use empty strings (e.g. if you want to get a player dummy)

    Player player = new Player("","");
    

    or you should give the new player instance the name and type you want to, so for example

    Player player = new Player("flashdrive2049","Player");
    

    Regards.

    0 讨论(0)
  • 2021-01-15 04:17

    When your class explicitly defines constructor, implicit no-arg constructor won't be created.

    You have explicit constructor in your class

    public Player(String nickName, String playerType) 
    {
    
        nick = nickName;
        type = playerType;
        health = 100;
        System.out.println("Welcome " + nick +" the " + type + ". I hope you are ready for an adventure!");
    }
    

    And trying to invoke no-arg constructor

     Player player = new Player();
    

    Either you need to pass parameters in above code (or) create no-arg constructor.

    0 讨论(0)
提交回复
热议问题