我该如何用颤抖的方式写出来? 我已经迅速制作了这个应用程序,现在我正在扑朔迷离地制作该应用程序,这是获得十二生肖的最佳方法。谢谢
let calendar = Calendar.current
let d = calendar.component(.day, from: date)
let m = calendar.component(.month, from: date)
switch (d,m) {
case (21...31,1),(1...19,2):
return "aquarius"
case (20...29,2),(1...20,3):
return "pisces"
case (21...31,3),(1...20,4):
return "aries"
case (21...30,4),(1...21,5):
return "taurus"
case (22...31,5),(1...21,6):
return "gemini"
case (22...30,6),(1...22,7):
return "cancer"
case (23...31,7),(1...22,8):
return "leo"
case (23...31,8),(1...23,9):
return "virgo"
case (24...30,9),(1...23,10):
return "libra"
case (24...31,10),(1...22,11):
return "scorpio"
case (23...30,11),(1...21,12):
return "sagittarius"
default:
return "capricorn"
}
答案 0 :(得分:2)
找到基本解决方案。
String getZodicaSign(DateTime date) {
var days = date.day;
var months = date.month;
if (months == 1) {
if (days >= 21) {
return "Aquarius";
}else {
return "Capricorn";
}
}else if (months == 2) {
if (days >= 20) {
return "Picis";
}else {
return "Aquarius";
}
}else if (months == 3) {
if (days >= 21) {
return "Aries";
}else {
return "Pisces";
}
}else if (months == 4) {
if (days >= 21) {
return "Taurus";
}else {
return "Aries";
}
}else if (months == 5) {
if (days >= 22) {
return "Gemini";
}else {
return "Taurus";
}
}else if (months == 6) {
if (days >= 22) {
return "Cancer";
}else {
return "Gemini";
}
}else if (months == 7) {
if (days >= 23) {
return "Leo";
}else {
return "Cancer";
}
}else if (months == 8) {
if (days >= 23) {
return "Virgo";
}else {
return "Leo";
}
}else if (months == 9) {
if (days >= 24) {
return "Libra";
}else {
return "Virgo";
}
}else if (months == 10) {
if (days >= 24) {
return "Scorpio";
}else {
return "Libra";
}
}else if (months == 11) {
if (days >= 23) {
return "Sagittarius";
}else {
return "Scorpio";
}
}else if (months == 12) {
if (days >= 22) {
return "Capricorn";
}else {
return "Sagittarius";
}
}
return "";
}
答案 1 :(得分:1)
我有这样一个实体:
class Sign {
Sign._(
this.name,
this.startDate,
this.endDate,
);
static final Sign ARIES = Sign._(
'Aries',
DateTime(0, DateTime.march, 21),
DateTime(0, DateTime.april, 19),
);
static final Sign TAURUS = Sign._(
'Taurus',
DateTime(0, DateTime.april, 20),
DateTime(0, DateTime.may, 20),
);
static final Sign GEMINI = Sign._(
'Gemini',
DateTime(0, DateTime.may, 21),
DateTime(0, DateTime.june, 20),
);
static final Sign CANCER = Sign._(
'Cancer',
DateTime(0, DateTime.june, 21),
DateTime(0, DateTime.july, 22),
);
static final Sign LEO = Sign._(
'Leo',
DateTime(0, DateTime.july, 23),
DateTime(0, DateTime.august, 22),
);
static final Sign VIRGO = Sign._(
'Virgo',
DateTime(0, DateTime.august, 23),
DateTime(0, DateTime.september, 22),
);
static final Sign LIBRA = Sign._(
'Libra',
DateTime(0, DateTime.september, 23),
DateTime(0, DateTime.october, 22),
);
static final Sign SCORPIO = Sign._(
'Scorpio',
DateTime(0, DateTime.october, 23),
DateTime(0, DateTime.november, 21),
);
static final Sign SAGITTARIUS = Sign._(
'Sagittarius',
DateTime(0, DateTime.november, 22),
DateTime(0, DateTime.december, 21),
);
static final Sign CAPRICORN = Sign._(
'Capricorn',
DateTime(0, DateTime.december, 22),
DateTime(0, DateTime.january, 19),
);
static final Sign AQUARIUS = Sign._(
'Aquarius',
DateTime(0, DateTime.january, 20),
DateTime(0, DateTime.february, 18),
);
static final Sign PISCES = Sign._(
'Pisces',
DateTime(0, DateTime.february, 19),
DateTime(0, DateTime.march, 20),
);
final String name;
final DateTime startDate;
final DateTime endDate;
static List<Sign> toList() => <Sign>[
ARIES,
TAURUS,
GEMINI,
CANCER,
LEO,
VIRGO,
LIBRA,
SCORPIO,
SAGITTARIUS,
CAPRICORN,
AQUARIUS,
PISCES,
];
static Sign fromName(String name) {
return toList().firstWhere(
(Sign sign) => name != null && sign.name.trim().toLowerCase() == name.trim().toLowerCase(),
orElse: () => null,
);
}
@override
String toString() => name;
}
和确定功能:
Sign determineSign(DateTime date) {
if ((date.month == DateTime.december && date.day >= Sign.CAPRICORN.startDate.day) ||
(date.month == DateTime.january && date.day <= Sign.CAPRICORN.endDate.day)) {
return Sign.CAPRICORN;
} else {
return Sign.toList().where((Sign sign) => _isDateInSign(sign, date)).first;
}
}
bool _isDateInSign(Sign sign, DateTime date) {
return sign.startDate.month * 100 + sign.startDate.day <= date.month * 100 + date.day &&
date.month * 100 + date.day <= sign.endDate.month * 100 + sign.endDate.day;
}
答案 2 :(得分:1)
你可以那样做
getZodiacSign(int day, int month) {
if((month == 1 && day <= 20) || (month == 12 && day >=22)) {
return "capricorn";
} else if ((month == 1 && day >= 21) || (month == 2 && day <= 18)) {
return "aquarius";
} else if((month == 2 && day >= 19) || (month == 3 && day <= 20)) {
return "pisces";
} else if((month == 3 && day >= 21) || (month == 4 && day <= 20)) {
return "aries";
} else if((month == 4 && day >= 21) || (month == 5 && day <= 20)) {
return "taurus";
} else if((month == 5 && day >= 21) || (month == 6 && day <= 20)) {
return "gemini";
} else if((month == 6 && day >= 21) || (month == 7 && day <= 22)) {
return "cancer";
} else if((month == 7 && day >= 23) || (month == 8 && day <= 23)) {
return "leo";
} else if((month == 8 && day >= 24) || (month == 9 && day <= 23)) {
return "virgo";
} else if((month == 9 && day >= 24) || (month == 10 && day <= 23)) {
return "libra";
} else if((month == 10 && day >= 24) || (month == 11 && day <= 22)) {
return "scorpio";
} else if((month == 11 && day >= 23) || (month == 12 && day <= 21)) {
return "sagittarius";
} else {
return "unknown";
}
}
答案 3 :(得分:1)
String getZodiacSign(DateTime birthdate) {
const List<String> signNames = [
"Capricorn",
"Aquarius",
"Pisces",
"Aries",
"Taurus",
"Gemini",
"Cancer",
"Leo",
"Virgo",
"Libra",
"Scorpio",
"Sagittarius",
"Capricorn"
];
const List<int> signDays = [0, 22, 20, 21, 21, 22, 23, 23, 23, 23, 23, 22, 22];
if (birthdate.day < signDays[birthdate.month])
return signNames[birthdate.month - 1];
else
return signNames[birthdate.month];
}