[英]Given this outline what is wrong with the code I wrote? please be explicit in explaining why something doesn't work
Write the definition of a class Player containing: An instance variable name of type String , initialized to the empty String. 编写类Player的定义,其中包含:一个类型为String的实例变量名称,初始化为空String。 An instance variable score of type int , initialized to zero.
类型为int的实例变量score,初始化为零。
A method called setName that has one parameter, whose value it assigns to the instance variable name . 一种名为setName的方法,它具有一个参数,该参数的值分配给实例变量name。
A method called setScore that has one parameter, whose value it assigns to the instance variable score . 一种名为setScore的方法,该方法具有一个参数,它将其值分配给实例变量score。
A method called getName that has no parameters and that returns the value of the instance variable name . 名为getName的方法,该方法没有参数,并且返回实例变量名称的值。
A method called getScore that has no parameters and that returns the value of the instance variable score . 一种名为getScore的方法,该方法没有参数,并且返回实例变量score的值。
No constructor need be defined. 无需定义构造函数。
public class Player{
private String name;
private int score = 0;
public void setName(String nm)
{name = nm;}
public void setScore(int sc)
{score = sc;}
public String getName()
{return name;}
public int getScore()
{return score;}
}
thank you. 谢谢。
(I'm not going to be "explicit" in terms of just giving you the answer, but hopefully I can help you fix it for yourself...) (就提供答案而言,我不会“明确”,但希望我能帮助您自己解决问题……)
Well, here's the start of what you've said you're meant to do: 好吧,这就是您所说的打算要开始的事情:
Write the definition of a class Player containing: An instance variable name of type String , initialized to the empty String.
编写类Player的定义,其中包含:一个类型为String的实例变量名称,初始化为空String。
Your code: 您的代码:
private String name;
私有字符串名称;
In what way is that initialized to the empty string? 以哪种方式将其初始化为空字符串?
To think about it another way: what would you expect the result of calling length()
on an empty string to be? 换种方式思考:您希望在空字符串上调用
length()
的结果是什么? What happens if you try calling it on your variable? 如果尝试在变量上调用它会怎样?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.