Java-找不到符号构造函数 [英] Java - Cannot find symbol constructor

查看:254
本文介绍了Java-找不到符号构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java的新手,如果我的问题很愚蠢,对不起.我正在从事这项工作,并且我已经阅读了几个小时的主要方法,但是我无法弄清楚.我在下面放了一些代码.我可能离这里很远,但是我希望完成的工作是获取启动构造函数的主要方法,但是在编译时出现错误,提示找不到符号-构造函数Player".现在,我想这与构造函数的字符串参数有关,但我全力以赴.如果有人可以阐明这个可能很简单的问题,我将非常高兴:)

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 figure it out. I put some of my code below. I might be way off here, but what I'm hoping to accomplish is to get the main method to start the constructor, but when I compile I get an error saying "cannot find symbol - constructor Player". Now, Im guessing this has something to do with the string parameters of the constructor, but I'm all out. If anyone could shed some light on this, probably very simple problem, I'd be very happy :)

public class Player {
private String nick;
private String type;
private int health;


public static void main(String[] args)
{
    Player player = new Player();
    player.print();
}


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!");
}

   public void print()
{
    System.out.println("Name: " + nick);
    System.out.println("Class: " + type);
    System.out.println("Remanining Health: " + health);
}

推荐答案

Player没有no-arg构造函数,可以使用:

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

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

如果您希望提示用户输入Player参数,您可以这样阅读:

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);

这篇关于Java-找不到符号构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆