java编译报错:实际参数列表和形式参数列表长度不同
importjava.util.*;importjava.util.Calendar;publicclassStudent{intid,Byear;doubleeng,m...
import java.util.*;
import java.util.Calendar;
public class Student {
int id, Byear;
double eng,math,com;
public Student(int id, int birthyear, double english,double math,double computer){
this.id = id;
this.Byear = Byear;
this.eng = eng;
this.math = math;
this.com = com;
};
public int getId(){
return id;
}
public int getBYear(){
return Byear;
}
public double getEng(){
return eng;
}
public double getMath(){
return math;
}
public double getCom(){
return com;
}
public double getSum(){
return this.eng + this.math + this.com;
}
public int getAge(Date Byear) throws Exception {
Calendar cal = Calendar.getInstance();
if (cal.before(Byear)) {
throw new IllegalArgumentException(
"The birthYear is before Now.It's unbelievable!");
}
int yearNow = cal.get(Calendar.YEAR);
cal.setTime(Byear);
int yearBirth = cal.get(Calendar.YEAR);
int age = yearNow - yearBirth;
return age;
}
public static void main(String[] args){
Date Byear = new Date();
System.out.println(Byear);
Student stu = new Student(1054,1993,78,84,90);
stu.getSum();
stu.getAge();
System.out.println(stu.toString());
}
} 展开
import java.util.Calendar;
public class Student {
int id, Byear;
double eng,math,com;
public Student(int id, int birthyear, double english,double math,double computer){
this.id = id;
this.Byear = Byear;
this.eng = eng;
this.math = math;
this.com = com;
};
public int getId(){
return id;
}
public int getBYear(){
return Byear;
}
public double getEng(){
return eng;
}
public double getMath(){
return math;
}
public double getCom(){
return com;
}
public double getSum(){
return this.eng + this.math + this.com;
}
public int getAge(Date Byear) throws Exception {
Calendar cal = Calendar.getInstance();
if (cal.before(Byear)) {
throw new IllegalArgumentException(
"The birthYear is before Now.It's unbelievable!");
}
int yearNow = cal.get(Calendar.YEAR);
cal.setTime(Byear);
int yearBirth = cal.get(Calendar.YEAR);
int age = yearNow - yearBirth;
return age;
}
public static void main(String[] args){
Date Byear = new Date();
System.out.println(Byear);
Student stu = new Student(1054,1993,78,84,90);
stu.getSum();
stu.getAge();
System.out.println(stu.toString());
}
} 展开
3个回答
展开全部
以下是我给你修改的代码,有注释的行是你需要改的。纯手写,求财富值!(下种子用...T_T)
import java.util.*;
import java.util.Calendar;
public class Student {
int id, Byear;
double eng,math,com;
public Student(int id, int Byear, double eng,double math,double com){ // 参数列表要这么改
this.id = id;
this.Byear = Byear;
this.eng = eng;
this.math = math;
this.com = com;
};
public int getId(){
return id;
}
public int getBYear(){
return Byear;
}
public double getEng(){
return eng;
}
public double getMath(){
return math;
}
public double getCom(){
return com;
}
public double getSum(){
return this.eng + this.math + this.com;
}
public int getAge(int Byear) throws Exception { //Byear 参数类型改为int
Calendar cal = Calendar.getInstance();
if ( cal.get(Calendar.YEAR) < Byear) { //把当前年份和输入年份比较
throw new IllegalArgumentException(
"The birthYear is before Now.It's unbelievable!");
}
int yearNow = cal.get(Calendar.YEAR); //得到当前年份
// cal.setTime(Byear); 不要了
// int yearBirth = cal.get(Calendar.YEAR); 不要了
int age = yearNow - Byear; //相减得到年龄
return age;
}
public static void main(String[] args){
Date NowYear = new Date(); // 输出当前时间
System.out.println(NowYear);
Student stu = new Student(1054,1993,78,84,90); //这时按照你的构造函数实例化Student类
System.out.println("总成绩:"+stu.getSum());
System.out.println("年龄:"+stu.getAge());
//System.out.println(stu.toString()); 你没有@Override toString()函数,别这么写
}
}
import java.util.*;
import java.util.Calendar;
public class Student {
int id, Byear;
double eng,math,com;
public Student(int id, int Byear, double eng,double math,double com){ // 参数列表要这么改
this.id = id;
this.Byear = Byear;
this.eng = eng;
this.math = math;
this.com = com;
};
public int getId(){
return id;
}
public int getBYear(){
return Byear;
}
public double getEng(){
return eng;
}
public double getMath(){
return math;
}
public double getCom(){
return com;
}
public double getSum(){
return this.eng + this.math + this.com;
}
public int getAge(int Byear) throws Exception { //Byear 参数类型改为int
Calendar cal = Calendar.getInstance();
if ( cal.get(Calendar.YEAR) < Byear) { //把当前年份和输入年份比较
throw new IllegalArgumentException(
"The birthYear is before Now.It's unbelievable!");
}
int yearNow = cal.get(Calendar.YEAR); //得到当前年份
// cal.setTime(Byear); 不要了
// int yearBirth = cal.get(Calendar.YEAR); 不要了
int age = yearNow - Byear; //相减得到年龄
return age;
}
public static void main(String[] args){
Date NowYear = new Date(); // 输出当前时间
System.out.println(NowYear);
Student stu = new Student(1054,1993,78,84,90); //这时按照你的构造函数实例化Student类
System.out.println("总成绩:"+stu.getSum());
System.out.println("年龄:"+stu.getAge());
//System.out.println(stu.toString()); 你没有@Override toString()函数,别这么写
}
}
更多追问追答
追答
public int getAge() throws Exception { //参数不要了 Calendar cal = Calendar.getInstance(); if ( cal.get(Calendar.YEAR) < Byear) { //把当前年份和输入年份比较 throw new IllegalArgumentException( "The birthYear is before Now.It's unbelievable!"); } int yearNow = cal.get(Calendar.YEAR); //得到当前年份 int age = yearNow - this.Byear; //相减得到年龄,注意这个地方用this.Byearreturn age; }
展开全部
public int getAge(Date Byear) 这个方法需要一个Date类型的参数,但你在main方法里调用的时候,stu.getAge();是这样调用的,并没有给他一个Date类型的参数,所以报错
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
stu.getAge(); 这个方法在使用的时候要传一个Date类型的参数,你没传 ,所以出错
public int getAge(Date Byear) throws Exception
public int getAge(Date Byear) throws Exception
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询