JAVA小菜鸟,来寻求大神的帮助,非常菜,请多包涵

2025-05-19 08:52:06
推荐回答(5个)
回答(1):

/*你写的那个完全不符合java正常编程规范,可以参考下下面这个*/
class Teacher
{
    private String teaName;
    private int teaAge;
    static Scanner input = new Scanner(System.in);
    /**
     * 输入信息
     */
    public void scan()
    {
        System.out.println("请输入教师名字:");
        setTeaName(input.next());
        System.out.println("请输入教师年龄:");
        setTeaAge(input.nextInt());
    }
    /**
     * 输出信息
     */
    public void printInfo()
    {
        System.out.println("教师姓名:" + this.teaName);
        if (this.teaAge > 18)
        {
            System.out.println("教师年龄:" + this.teaAge);
        }
        else
        {
            System.out.println("教师龄输入不正确,系统默认18!\n教师龄:18");
        }
    }
    public String getTeaName()
    {
        return teaName;
    }
    public void setTeaName(String teaName)
    {
        this.teaName = teaName;
    }
    public int getTeaAge()
    {
        return teaAge;
    }
    public void setTeaAge(int teaAge)
    {
        this.teaAge = teaAge;
    }
}
/** 主方法 */
public class demo
{
    public static void main(String[] args)
    {
        Teacher teastr = new Teacher();
        teastr.scan();
        teastr.printInfo();
    }
}

回答(2):

因为你的scan方法传入了2个参数,然而我们输入的值只不过是改变了参数的值,并未改变对象的属性,所以就没有值输出了,况且你还把2个属性给封装了需要在方法中调用set方法才能给对象的属性赋值,在同一类调用不通的方法是可以的。只需要在方法中new出来对象就可以互相调用了。

回答(3):

你这段代码要干什么我没有看明白你的意思你在前面调了个get方法在后面再set中写了判断这个这个没明白然后你又在主方法中调用了这个方法还给他传了两个空值

回答(4):

public void setTeaName(String teaName) {
this.teaName = input.next();
System.out.println("教师姓名:" +this.teaName);
}
这段代码有问题,方法的形参是teaName,如果要给属性teaName赋值,应该写this.teaName =teaName;
其它几段也有同样的问题

回答(5):

package com.ceair.route.login.test;

import java.util.Scanner;

class Teacher {
private String scan;
private String teaName;
private int teaAge;
static Scanner input = new Scanner(System.in);

public void scan(String teaName,int teaAge) {
System.out.println("请输入教师名字:");
this.setTeaName(teaName);
System.out.println("请输入教师年龄:");
this.setTeaAge(teaAge);
}

public String getTeaName() {
return teaName;
}

public void setTeaName(String teaName) {
this.teaName = input.next();
System.out.println("教师姓名:" + this.teaName);
}

public int getTeaAge() {
return teaAge;
}

public void setTeaAge(int teaAge) {
this.teaAge = input.nextInt();
if (this.teaAge > 18) {
System.out.println("教师年龄:" + this.teaAge);
} else {
System.out.println("教师年龄输入不正确,系统默认为18!\n教师年龄:18");
}
}
}

public class Demo {
public static void main(String[] args) {
Teacher teastr = new Teacher();
teastr.scan(null,0);
teastr.getTeaName();
teastr.getTeaAge();

}
}