![](/img/trans.png)
[英]Assist me in finding the error: invalid method declaration; return type required
[英]below program we have one error invalid method declaration return type required pls explain the program i need o/p
class 学生{公共字符串名称; 公共 int id; 公共双标;
公共学生(字符串名称,int id,双标记){ this.name=name; 这个.id=id; this.marks=标记; } } class 程序 { public static void main(String[] args) { student s1=new student("vasu",12,12.21); System.out.println("name="+s1.name,"id="+s1.id,"marks="+s1.marks); } }
首先用```包围你的代码,这样它就不会像那样堆积起来
你的程序有几个问题:
学生构造函数的名称应与 class 完全相同
System.out.println()
接受一个字符串作为参数而不是三个
纠正上述错误后,我们有
public class Student {
public String name;
public int id;
public double marks;
public Student(String name, int id, double marks) {
this.name=name;
this.id=id;
this.marks=marks;
}
public static void main(String[] args) {
Student s1=new Student("vasu",12,12.21);
System.out.println("name="+s1.name+"id="+s1.id+"marks="+s1.marks);
}
}
Java 区分大小写。 在声明 class 时,您使用了“学生”,而在声明构造函数时,您使用了“学生”。 因此 Java 将 Student 视为一种方法。 所以它说返回类型是必需的。 命名 class 时,首字母大写,构造函数使用相同。
class Student {
public String name;
public int id;
public double marks;
public Student (String name, int id, double marks) {
this.name=name;
this.id=id;
this.marks=marks;
}
}
还有另一个问题。您已经为 System.out.println 传递了三个字符串,其中它只接受一个字符串。应该更正为,
System.out.println("name="+s1.name + "id="+s1.id + "marks="+s1.marks);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.