我希望用户键入一些输入,setter方法在另一个类上,但我不知道如何编写代码。我错过了什么或犯了一些错误吗?
这是我的代码。
Project.java
public class Project
{
private String projectName;
//constructor
public Project()
{
projectName = "No Project";
}
//projectName setter
public void setProjectName(String projectName)
{
this.projectName = projectName;
}
//projectName getter
public String getProjectName()
{
return projectName;
}
//display info
public void displayInfo ()
{
System.out.println("**PROJECT DETAILS**");
System.out.println("Project Name : " + projectName);
}
} //close class
DisplayProject.java
public class DisplayProject
{
public static void main(String[] args)
{
//create object
Project createProject = new Project();
Scanner input = new Scanner (System.in);
//enter project name
System.out.print("Enter project name: ");
createProject.setProjectName(projectName) = input.nextLine(); <Here is my problem>
} // close main class
} //close class
答案 0 :(得分:1)
成功
createProject.setProjectName(input.nextLine());
而不是
createProject.setProjectName(projectName) = input.nextLine();
你也可以用这种方式做得更有意义
String projectName = input.nextLine(); // read a line
createProject.setProjectName(projectName);
答案 1 :(得分:0)
你应该做
createProject.setProjectName(input.nextLine());
您所做的是语法不正确。它甚至不会编译。