文件1.Student.java
公开课学生{
// Fill the code
private int studentId;
private String studentName, studentAddress, collegeName;
public int getStudentId(){
return studentId;
}
public String getStudentName(){
return studentName;
}
public String getStudentAddress(){
return studentAddress;
}
public String getCollegeName(){
return collegeName;
}
public Student(int sid, String sname, String sadd){
studentId = sid;
studentName = sname;
collegeName="NIT";
studentAddress=sadd;
}
public Student(int sid, String sname, String sadd, String cname){
studentId = sid;
studentName = sname;
collegeName = cname;
studentAddress = sadd;
}
}
文件2.UserInterface.java
导入java.util.Scanner; 公共类用户界面{
public static void main(String[] arg)
{
// Fill the code
Scanner sc = new Scanner(System.in);
int flag;
String cname="";
System.out.println("Enter Student's Id:");
int sid=sc.nextInt();
sc.nextLine();
System.out.println("Enter Student's Name:");
String sname=sc.nextLine();
System.out.println("Enter Student's address:");
String sadd = sc.nextLine();
while(true){
System.out.println("Whether the student is from NIT(Yes/No):");
String check=sc.nextLine();
if(check.equalsIgnoreCase("yes")){
flag=1;
break;
}
else{
if(check.equalsIgnoreCase("no")){
flag=0;
System.out.println("Enter the college name:");
cname = sc.nextLine();
break;
}
else
System.out.println("Wrong input");
}
}
if(flag==1){
Student s = new Student(sid,sname,sadd);
System.out.println("Student id:"+s.getStudentId());
System.out.println("Student name:"+s.getStudentName());
System.out.println("Address:"+s.getStudentAddress());
System.out.println("College name:"+s.getCollegeName());
}
else{
Student s = new Student(sid,sname,sadd,cname);
System.out.println("Student id:"+s.getStudentId());
System.out.println("Student name:"+s.getStudentName());
System.out.println("Address:"+s.getStudentAddress());
System.out.println("College name:"+s.getCollegeName());
}
}
}
编译代码时运行,但在提交代码进行评估后显示错误 即失败 1 -- test2_ChecktestForFourArgumentConstructors:: 检查对象是否正确创建
失败 2 -- test1_CheckForThreeArgumentConstructors:: 检查对象是否正确创建
那么为什么在提交时会显示这些错误。以及如何解决这些问题。另外,错误实际上想描述/告诉什么。
请帮忙
我希望代码能够无缝运行,但它显示了如上所述的错误。那么我们如何解决这些错误以使代码正常运行
确保您的构造函数已正确定义并且它们初始化了所有必要的字段。您的
Student
类中有两个构造函数:一个需要三个参数,另一个需要四个参数
public class UserInterface {
public static void main(String[] arg) {
Student s; // Declaration moved outside of if-else block to ensure visibility
if(flag == 1){
s = new Student(sid, sname, sadd); // Use three-argument constructor
} else {
s = new Student(sid, sname, sadd, cname); // Use four-argument constructor
}
sc.close();
}
}