gpt4 book ai didi

java - 我必须在 blueJ 中创建 3 个对象并调用 display() 但我什至无法创建对象。请看代码结果

转载 作者:行者123 更新时间:2023-12-01 17:53:38 25 4
gpt4 key购买 nike

我必须在blueJ中制作3个对象并调用display(),但我什至无法制作对象。请查看代码结果

public class Project
{
String Name;
String deadline;
String Version;
double cost;
public Project(String Name,String deadline,double cost){
this.Name=Name;
this.deadline=deadline;
this.cost=cost;
}
public void display(){
System.out.print("Name of project is: "+this.Name);
System.out.print("Deadline of the project is: "+this.deadline);
System.out.print("Cost of the project is: Rs."+this.cost);
}


}

在尝试创建对象时,会出现一个对话框,其中显示“即使在输入后仍应预期,所以我不知道在哪里输入”

最佳答案

我没有正确理解你的问题...如果你想创建该类的三个对象并希望输出构造函数中传递的属性,你可以这样做:

public static void main(String[] args) {
List<Project> projects = new ArrayList<>();
Project pro1 = new Project("Project - X", "deadline - x", 55.12);
Project pro2 = new Project("Project - Y", "deadline - y", 23655.2);
Project pro3 = new Project("Project - Z", "deadline - z", 100.00);
projects.add(pro1);
projects.add(pro2);
projects.add(pro3);

display(projects);


}
static void display(List<Project> pro) {
for (Project project : pro) {
System.out.println("Name of project is: " + project.name);
System.out.println("Deadline of the project is: " + project.deadline);
System.out.println("Cost of the project is: Rs." + project.cost);
System.out.println();

}
}

public static class Project {
String name;
String deadline;
String version;
double cost;

public Project(String name, String deadline, double cost) {
this.name = name;
this.deadline = deadline;
this.cost = cost;
}
}

输出:

Name of project is: Project - X Deadline of the project is: deadline - x Cost of the project is: 55.12

Name of project is: Project - Y Deadline of the project is: deadline - y Cost of the project is: 23655.2

Name of project is: Project - Z Deadline of the project is: deadline - z Cost of the project is: 100.0

关于java - 我必须在 blueJ 中创建 3 个对象并调用 display() 但我什至无法创建对象。请看代码结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60755531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com