简体   繁体   English

有了这个大纲,我编写的代码有什么问题? 请明确说明为什么不起作用

[英]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.

相关问题 我已经开始学习线程同步。我写了一些代码。为什么有时同步块不起作用? - I have started learning synchronization in threading.I wrote some code.Why sometimes synchronized block doesn't work right? 我写了以下几行代码来更改此类的xml文件,但是它不起作用 - I wrote this few lines of code to change the xml file of this class but it doesn't work Java:Paint应用程序,代码不起作用,我在做什么错? - Java: Paint application, the code doesn't work, what am I doing wrong? instanceof似乎不起作用-我的代码有什么问题? - instanceof doesn't seem to work - what's wrong with my code? TreeSet不起作用,以下代码有什么问题? - TreeSet doesn't work, what's wrong with the following code? \\ n不起作用? 我的代码有什么问题? - \n doesn't work? What's wrong in my code? 请我在这段代码中做错了什么 - Please what am i doing wrong in this code 我想在执行if块时向用户(移动应用程序)发出通知。 此代码无效,我的代码有什么问题? - I want to give notification to the user (mobile application) when the if block is execute. this code doesn't work, what is wrong with my code? Java中的初学者 - 为什么这个Java代码不起作用:Math.sqrt需要识别什么? - Beginner in Java - Why doesn't this Java code work: what do I need for Math.sqrt to be recognized? 为什么我的代码对CardLayout不起作用? 不管我做什么,它只会显示一个空白的JFrame - Why doesn't my code work for CardLayout? Not matter what I do, it just shows a blank JFrame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM