想向你请教一下Syntax error on token "int", @ expected这样的错误怎么改?谢谢了
publicclassPerson{privateStringname;privateintage;publicPerson(Stringname,intage){thi...
public class Person {
private String name;
private int age;
public Person(String name,int age){
this.setName(name);
this.setAge(age);
}
public void setName(String name) {
if(name==null||name=="")
this.name="姓名未知";
else
this.name=name;
}
public String getName(){
return this.name;
}
public void setAge(int age) {
try{
if(age>0&&age<100)
this.age=age;
else
throw new Exception("年龄无效");
}
catch(Exception e){
System.out.println(e.toString());
}
public int getAge(){
return this.age;
}
public String toString(){
return getName()+","+getAge()+"岁";
}
public int olderThen(Person p2){
return this.getAge()-p2.getAge();
}
public static void main(String[] args){
Person p1=new Person("小李子",36);
System.out.println(p1.toString());
Person p2=new Person("小贾",26);
System.out.println(p2.toString());
System.out.println(p1.getName()+"比"+p2.getName()+"大"+p1.olderThen(p2)+"岁");
}
}
} 展开
private String name;
private int age;
public Person(String name,int age){
this.setName(name);
this.setAge(age);
}
public void setName(String name) {
if(name==null||name=="")
this.name="姓名未知";
else
this.name=name;
}
public String getName(){
return this.name;
}
public void setAge(int age) {
try{
if(age>0&&age<100)
this.age=age;
else
throw new Exception("年龄无效");
}
catch(Exception e){
System.out.println(e.toString());
}
public int getAge(){
return this.age;
}
public String toString(){
return getName()+","+getAge()+"岁";
}
public int olderThen(Person p2){
return this.getAge()-p2.getAge();
}
public static void main(String[] args){
Person p1=new Person("小李子",36);
System.out.println(p1.toString());
Person p2=new Person("小贾",26);
System.out.println(p2.toString());
System.out.println(p1.getName()+"比"+p2.getName()+"大"+p1.olderThen(p2)+"岁");
}
}
} 展开
1个回答
展开全部
public void setAge(int age) {
try{
if(age>0&&age<100)
this.age=age;
else
throw new Exception("年龄无效");
}
catch(Exception e){
System.out.println(e.toString());
}
这个方法结尾少了个 }
而在
public static void main(String[] args){
Person p1=new Person("小李子",36);
System.out.println(p1.toString());
Person p2=new Person("小贾",26);
System.out.println(p2.toString());
System.out.println(p1.getName()+"比"+p2.getName()+"大"+p1.olderThen(p2)+"岁");
}
}
}
这里多了个}
try{
if(age>0&&age<100)
this.age=age;
else
throw new Exception("年龄无效");
}
catch(Exception e){
System.out.println(e.toString());
}
这个方法结尾少了个 }
而在
public static void main(String[] args){
Person p1=new Person("小李子",36);
System.out.println(p1.toString());
Person p2=new Person("小贾",26);
System.out.println(p2.toString());
System.out.println(p1.getName()+"比"+p2.getName()+"大"+p1.olderThen(p2)+"岁");
}
}
}
这里多了个}
来自:求助得到的回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询