/**
專業(yè)從事網(wǎng)站設(shè)計制作、成都做網(wǎng)站,高端網(wǎng)站制作設(shè)計,微信小程序,網(wǎng)站推廣的成都做網(wǎng)站的公司。優(yōu)秀技術(shù)團隊竭力真誠服務(wù),采用html5+CSS3前端渲染技術(shù),響應(yīng)式網(wǎng)站建設(shè),讓網(wǎng)站在手機、平板、PC、微信下都能呈現(xiàn)。建站過程建立專項小組,與您實時在線互動,隨時提供解決方案,暢聊想法和感受。
* 根據(jù)月日判斷星座
* @param month
* @param day
* @return int
*/
public static String getConstellation(int m,int d){
final String[] constellationArr = {"魔羯座" ,"水瓶座", "雙魚座", "牡羊座", "金牛座", "雙子座", "巨蟹座", "獅子座", "處女座", "天秤座","天蝎座", "射手座", "魔羯座" };
final int[] constellationEdgeDay = { 20,18,20,20,20,21,22,22,22,22,21,21};
int month=m;
int day =d;
if (day = constellationEdgeDay[month-1]) {
month = month - 1;
}
if (month = 0) {
return constellationArr[month];
}
//default to return 魔羯
return constellationArr[11];
}
定義一個String類;
使用接收格式為(xx/xx);
使用Stringbuffer分割為兩個int類型
month(月) day(日)
switch(month){
case 1:
if(day=23){
system.out.print("....星座");
}else{
system.out.print("....星座");
}
black;
…………
}
import?java.text.ParseException;
import?java.text.SimpleDateFormat;
import?java.util.Calendar;
import?java.util.Date;
import?java.util.Scanner;
public?class?Test?{
public?static?void?main(String[]?args)?throws?ParseException?{
Scanner?scanner?=?new?Scanner(System.in);
//?只輸入月、日,此代碼未校驗月、日輸入是否合理,比如2月30
System.out.print("請輸入你的出生月份");
int?month?=?scanner.nextInt();
System.out.print("請輸入你的出生日期");
int?day?=?scanner.nextInt();
System.out.print("你的星座是:");
//?通過輸入完整日期的方式判斷,可以自動處理不合理日期,比如2月30
//?SimpleDateFormat?dateFormat?=?new?SimpleDateFormat("yyyy-MM-dd");
//?System.out.print("請輸入你的出生日期(形如2018-02-03):");
//?Date?birthday?=?dateFormat.parse(scanner.nextLine());
//
//?Calendar?calendar?=?Calendar.getInstance();
//?calendar.setTime(birthday);
//?int?month?=?calendar.get(Calendar.MONTH)?+?1;
//?int?day?=?calendar.get(Calendar.DAY_OF_MONTH);
//?System.out.print("你的生日是?:"?+?dateFormat.format(birthday)?+?"\r\n你的星座是:");
scanner.close();
switch?(month)?{
case?1:
if?(day??22)
System.out.println("摩羯座");
else
System.out.println("水瓶座");
break;
case?2:
if?(day??22)
System.out.println("水瓶座");
else
System.out.println("雙魚座");
break;
case?3:
if?(day??22)
System.out.println("雙魚座");
else
System.out.println("白羊座");
break;
case?4:
if?(day??22)
System.out.println("白羊座");
else
System.out.println("金牛座");
break;
case?5:
if?(day??22)
System.out.println("金牛座");
else
System.out.println("雙子座");
break;
case?6:
if?(day??22)
System.out.println("雙子座");
else
System.out.println("巨蟹座");
break;
case?7:
if?(day??22)
System.out.println("巨蟹座");
else
System.out.println("獅子座");
break;
case?8:
if?(day??22)
System.out.println("獅子座");
else
System.out.println("處女座");
break;
case?9:
if?(day??22)
System.out.println("處女座");
else
System.out.println("天枰座");
break;
case?10:
if?(day??22)
System.out.println("天枰座");
else
System.out.println("天蝎座");
break;
case?11:
if?(day??22)
System.out.println("天蝎座");
else
System.out.println("射手座");
break;
case?12:
if?(day??22)
System.out.println("射手座");
else
System.out.println("摩羯座");
}
}
}
既然不允許用其他的方式,那就直接上代碼吧....
public?static?void?main(String[]?args)?{
int?point?=?-1;
String[]?str?=?{"白羊","金牛","雙子","巨蟹","獅子","處女","天平","天蝎","射手","摩羯","水瓶","雙魚"};
SimpleDateFormat?sdf?=?new?SimpleDateFormat("MM-dd");
System.out.println("請輸入一個日期,格式為MM-dd,例如:8-9");
Scanner?scan?=?new?Scanner(System.in);
Date?d?=?null;
while?(true)?{
try?{
String?input?=?scan.next();
d?=?sdf.parse(input);
break;
}?catch?(Exception?ex)?{
System.out.println("請輸入正確的日期格式,例如:08-09");
}
}
//格式化日期,將日期轉(zhuǎn)成**.**的格式,比如1月1日轉(zhuǎn)成1.01
Double?date?=?Double.parseDouble((d.getMonth()?+?1)
+?"."?+?String.format("%02d",?d.getDate()));
if?(3.21?=?date??4.19?=?date)?{
point?=?0;
}?else?if?(4.20?=?date??5.20?=?date)?{
point?=?1;
}?else?if?(5.21?=?date??6.21?=?date)?{
point?=?2;
}?else?if?(6.22?=?date??7.22?=?date)?{
point?=?3;
}?else?if?(7.23?=?date??8.22?=?date)?{
point?=?4;
}?else?if?(8.23?=?date??9.22?=?date)?{
point?=?5;
}?else?if?(9.23?=?date??10.23?=?date)?{
point?=?6;
}?else?if?(10.24?=?date??11.22?=?date)?{
point?=?7;
}?else?if?(11.23?=?date??12.21?=?date)?{
point?=?8;
}?else?if?(12.22?=?date??12.31?=?date)?{
point?=?9;
}?else?if?(1.01?=?date??1.19?=?date)?{
point?=?9;
}?else?if?(1.20?=?date??2.18?=?date)?{
point?=?10;
}?else?if?(2.19?=?date??3.20?=?date)?{
point?=?11;
}
if(point?==?-1)?{
System.out.println("你真的是地球人么....");return;
}
System.out.println(String.format(
"找到啦!o^?^o,你的星座是%s.",?str[point]));