设计一个星座类,并为其添加一个带参数的构造函数

it2023-04-09  67

public class Constellation { private String birthday; private int years; private int months; private int days; public Constellation(String birthday){ this.birthday = birthday; } public int getBornYear(){ return years; } public int getBornMonth(){ int months=Integer.parseInt(birthday.substring(4,6)); return months; } public int getBornDay(){ int days=Integer.parseInt(birthday.substring(6)); return days; } public static void main(String[] args) { Constellation constellation = new Constellation("19990724"); int months = constellation.getBornMonth(); int days = constellation.getBornDay(); String result = ""; switch (months) { case 1: result = days<21?"摩羯座":"水瓶座"; break; case 2: result = days<20?"水平座":"双鱼座"; break; case 3: result = days<21?"双鱼座":"白羊座"; break; case 4: result = days<21?"白羊座":"金牛座"; break; case 5: result = days<22?"金牛座":"双子座"; break; case 6: result = days<22?"双子座":"巨蟹座"; break; case 7: result = days<23?"巨蟹座":"狮子座"; break; case 8: result = days<24?"狮子座":"处女座"; break; case 9: result = days<24?"处女座":"天秤座"; break; case 10: result = days<24?"天秤座":"天蝎座"; break; case 11: result = days<23?"天秤座":"射手座"; break; case 12: result = days<22?"射手座":"摩羯座"; break; } System.out.println("你的星座是:"+result); } }

这是某套卷子的题~

最新回复(0)