标题:课堂作业急第一次跨语言不好意思啊用JAVA语言 谢谢谢谢谢谢了
只看楼主
JJGA
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2021-4-11
结帖率:0
 问题点数:0 回复次数:3 
课堂作业急第一次跨语言不好意思啊用JAVA语言 谢谢谢谢谢谢了
1.定义一个人的类,属性包括姓名、性别、身份证号,方法是根据身份证号获得出生年月日。通过将类转化为对象的方式输出如下格式:姓名,性别,出生年月日。

2.定义一个星座的类,属性为月份、日期,方法是根据输出的月日得到星座。 通过将类转化为对象的方式输出如下格式:几月几日是什么星座。
搜索更多相关主题的帖子: 作业 语言 星座 输出 JAVA 
2021-05-18 15:57
one737
Rank: 2
等 级:论坛游民
威 望:1
帖 子:19
专家分:18
注 册:2021-7-2
得分:0 
1.
程序代码:
public class Person {
    private String name;
    private int gender;//0男1女
    private String id;
    public Person(String name, int gender, String id) {
        this.name = name; this.gender = gender; this.id = id;
    }
    public String getName() {return name;}
    public void setName(String name) {this.name = name;}
    public int getGender() {return gender;}
    public void setGender(int gender) {this.gender = gender;}
    public String getId() {return id;}
    public void setId(String id) {this.id = id;}
    public String getBirthday() {//主要方法,默认按照18位身份证
        String nian = id.substring(6, 10);//
        String yue = id.substring(10, 12);//
        String ri = id.substring(12, 14);//
        String birth = nian + "年" + yue + "月" + ri + "日";//组合
        return birth;
    }
    @Override
    public String toString() {
        return getName() + "," + ((getGender() == 0) ? "男" : "女") + ",生于" + getBirthday();
    }
    public static void main(String[] args) {//测试
        Person s = new Person("one-737", 0, "11011119750606161X");
        System.out.println(s.toString());//输出:one-737,男,生于1975年06月06日
    }
}

2.
程序代码:
public class Star {
    private int month, day;
    public Star(int month, int day) {
        this.month = month;
        this.day = day;
    }
    public int getMonth() {return month;}
    public void setMonth(int month) {this.month = month;}
    public int getDay() {return day;}
    public void setDay(int day) {this.day = day;}
    private String getStarName() {
        switch (month) {
            case 1:
                if (day >= 21 && day <= 31) return "水瓶";
                else return "摩羯";
            case 2:
                if (day >= 20 && day <= 29) return "双鱼";
                else return "水瓶";
            case 3:
                if (day >= 21 && day <= 31) return "白羊";
                else return "双鱼";
            case 4:
                if (day >= 21 && day <= 30) return "金牛";
                else return "白羊";
            case 5:
                if (day >= 22 && day <= 31) return "双子";
                else return "金牛";
            case 6:
                if (day >= 22 && day <= 30) return "巨蟹";
                else return "双子";
            case 7:
                if (day >= 23 && day <= 31) return "狮子";
                else return "巨蟹";
            case 8:
                if (day >= 24 && day <= 31) return "处女";
                else return "狮子";
            case 9:
                if (day >= 24 && day <= 30) return "天秤";
                else return "处女";
            case 10:
                if (day >= 24 && day <= 31) return "天蝎";
                else return "天秤";
            case 11:
                if (day >= 23 && day <= 30) return "射手";
                else return "天蝎";
            case 12:
                if (day >= 22 && day <= 31) return "摩羯";
                else return "射手";
        }
    }
    public String getStar() {//打完上面的方法才发现没加座。。。懒得一个一个加就在这加了
        return getStarName() + "座";
    }
    public static void main(String[] args) {//测试
        Star s = new Star(6, 30);//6月30日;
        System.out.println(s.getStar());//应输出“巨蟹座”
    }
}
2021-07-02 11:14
one737
Rank: 2
等 级:论坛游民
威 望:1
帖 子:19
专家分:18
注 册:2021-7-2
得分:0 
第2个有错。。。修改版
程序代码:
public class Star {
    private int month, day;
    public Star(int month, int day) {
        this.month = month;
        this.day = day;
    }
    public int getMonth() {return month;}
    public void setMonth(int month) {this.month = month;}
    public int getDay() {return day;}
    public void setDay(int day) {this.day = day;}
    private String getStarName() {
        String str = "";
        switch (month) {
            case 1:
                if (day >= 21 && day <= 31) str = "水瓶";
                else str = "摩羯";
            case 2:
                if (day >= 20 && day <= 29) str = "双鱼";
                else str = "水瓶";
            case 3:
                if (day >= 21 && day <= 31) str = "白羊";
                else str = "双鱼";
            case 4:
                if (day >= 21 && day <= 30) str = "金牛";
                else str = "白羊";
            case 5:
                if (day >= 22 && day <= 31) str = "双子";
                else str = "金牛";
            case 6:
                if (day >= 22 && day <= 30) str = "巨蟹";
                else str = "双子";
            case 7:
                if (day >= 23 && day <= 31) str = "狮子";
                else str = "巨蟹";
            case 8:
                if (day >= 24 && day <= 31) str = "处女";
                else str = "狮子";
            case 9:
                if (day >= 24 && day <= 30) str = "天秤";
                else str = "处女";
            case 10:
                if (day >= 24 && day <= 31) str = "天蝎";
                else str = "天秤";
            case 11:
                if (day >= 23 && day <= 30) str = "射手";
                else str = "天蝎";
            case 12:
                if (day >= 22 && day <= 31) str = "摩羯";
                else str = "射手";
        }
        return str;
    }
    public String getStar() {//打完上面的方法才发现没加座。。。懒得一个一个加就在这加了
        return getStarName() + "座";
    }
    public static void main(String[] args) {//测试
        Star s = new Star(6, 30);//6月30日;
        System.out.println(s.getStar());//应输出“巨蟹座”
    }
}
2021-07-02 11:18
one737
Rank: 2
等 级:论坛游民
威 望:1
帖 子:19
专家分:18
注 册:2021-7-2
得分:0 
emmm,还是有错。。。二次修改版:
程序代码:
public class Star {
    private int month, day;
    public Star(int month, int day) {
        this.month = month;
        this.day = day;
    }
    public int getMonth() {return month;}
    public void setMonth(int month) {this.month = month;}
    public int getDay() {return day;}
    public void setDay(int day) {this.day = day;}
    private String getStarName() {
        String str = "";
        switch (month) {
            case 1:
                if (day >= 21 && day <= 31) str = "水瓶";
                else str = "摩羯";
                break;
            case 2:
                if (day >= 20 && day <= 29) str = "双鱼";
                else str = "水瓶";
                break;
            case 3:
                if (day >= 21 && day <= 31) str = "白羊";
                else str = "双鱼";
                break;
            case 4:
                if (day >= 21 && day <= 30) str = "金牛";
                else str = "白羊";
                break;
            case 5:
                if (day >= 22 && day <= 31) str = "双子";
                else str = "金牛";
                break;
            case 6:
                if (day >= 22 && day <= 30) str = "巨蟹";
                else str = "双子";
                break;
            case 7:
                if (day >= 23 && day <= 31) str = "狮子";
                else str = "巨蟹";
                break;
            case 8:
                if (day >= 24 && day <= 31) str = "处女";
                else str = "狮子";
                break;
            case 9:
                if (day >= 24 && day <= 30) str = "天秤";
                else str = "处女";
                break;
            case 10:
                if (day >= 24 && day <= 31) str = "天蝎";
                else str = "天秤";
                break;
            case 11:
                if (day >= 23 && day <= 30) str = "射手";
                else str = "天蝎";
                break;
            case 12:
                if (day >= 22 && day <= 31) str = "摩羯";
                else str = "射手";
                break;
        }
        return str;
    }
    public String getStar() {//打完上面的方法才发现没加座。。。懒得一个一个加就在这加了
        return getStarName() + "座";
    }
    public static void main(String[] args) {//测试
        Star s = new Star(6, 30);//6月30日;
        System.out.println(s.getStar());//应输出“巨蟹座”
    }
}
2021-07-02 11:20



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-505773-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.253391 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved