3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

星座判定(Java)

Last updated at Posted at 2015-12-03

作るきっかけとなったのは、Yahoo!知恵袋に投稿されていたこの質問でした。
ただ、ありえない日付が入力できるようになってたので、そんなことができないようにしました。

Main.java
import java.util.Calendar;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Main {
	public static void main(String[] args) throws IOException{
		CmdLineInput cmd = new CmdLineInput();
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, 2012);
		
		int month = cmd.getnum("誕生日の月を入力してください:", 12, 1);
		cal.set(Calendar.MONTH, month - 1);
		int day = cmd.getnum("誕生日の日を入力してください:", cal.getActualMaximum(Calendar.DATE), 1);
		cal.set(Calendar.DATE, day);
		Seiza seiza = new Seiza(cal);
		System.out.println("あなたの星座は" + seiza.getSeiza() + "です。");
	}
}
class Seiza {
	Calendar cal;
	Seiza (Calendar cal){
		this.cal = cal;
	}
	public String getSeiza(){
		if(this.checkInSeizaDateRange(1, 21, 2, 18)) return "水瓶座";
		else if(this.checkInSeizaDateRange(2, 19, 3, 20)) return "魚座";
		else if(this.checkInSeizaDateRange(3, 21, 4, 19)) return "牡羊座";
		else if(this.checkInSeizaDateRange(4, 20, 5, 20)) return "牡牛座";
		else if(this.checkInSeizaDateRange(5, 21, 6, 21)) return "双子座";
		else if(this.checkInSeizaDateRange(6, 22, 7, 22)) return "蟹座";
		else if(this.checkInSeizaDateRange(7, 23, 8, 22)) return "獅子座";
		else if(this.checkInSeizaDateRange(8, 23, 9, 22)) return "乙女座";
		else if(this.checkInSeizaDateRange(9, 23, 10, 23)) return "天秤座";
		else if(this.checkInSeizaDateRange(10, 24, 11, 22)) return "蠍座";
		else if(this.checkInSeizaDateRange(11, 23, 12, 21)) return "射手座";
		else return "山羊座";
	}
	private Calendar calFirst = Calendar.getInstance(), calLast = Calendar.getInstance();
	private boolean checkInSeizaDateRange(int firstDateMonth, int firstDateDay, int lastDateMonth, int lastDateDay){
		this.calFirst.set(2012, firstDateMonth - 1, firstDateDay);
		this.calLast.set(2012, lastDateMonth - 1, lastDateDay);
		return cal.get(Calendar.DAY_OF_YEAR) >= calFirst.get(Calendar.DAY_OF_YEAR) 
				&& cal.get(Calendar.DAY_OF_YEAR) <= calLast.get(Calendar.DAY_OF_YEAR);
	}
}
class CmdLineInput {
	BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
	public int getnum(String echo, int max, int min) throws IOException{
		if(max < min) return getnum(echo, min, max);
		System.out.print(echo);
		try{
			int i = Integer.parseInt(input.readLine());
			if(i > max || i < min) return getnum(echo, max, min);
			else return i;
		}catch(NumberFormatException e){
			return getnum(echo, max, min);
		}
	}
}

結構しょうもないミスかましてて作るのに1時間近くかかっちゃいましたが、なんとか完成しました。

3
3
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?