---
# System prepended metadata

title: 厄年計算課題
tags: [Practice, Java]

---

# 厄年計算課題

###### tags: `Java` `Practice`

1. Solution 1: 最差解
```
package test;

import java.util.*;

public class Test {

//Definition
public static final int sexMale = 1;
public static final int sexFemale = 2; 

public static void go() {

//initialization
String inputYear = null;
String inputSex = null;
int birthYear = 0;
int thisYear = 0;
int age = 0;
int sex = 0;
	
//Display input information and request user's input of birth year	
Scanner input = new Scanner(System.in);
System.out.println("貴方の生年（西暦４桁）を入力してください:");

//casting, get current year, calculate age
inputYear = input.nextLine();
birthYear = Integer.parseInt(inputYear);
thisYear = 2022;
age = thisYear - birthYear + 1;

//Display age information and request user's input of sexuality
System.out.println("貴方の年齢(数え年の年齢)は" + age + "歳です。");
System.out.println("");
System.out.println("貴方の性別を教えてください。（男性の場合1を、女性の場合2を入力してください。）");
inputSex = input.nextLine();

//casting, close input
sex = Integer.parseInt(inputSex);
input.close();

//Determination
if (sex == sexMale) {
	switch (age) {
	case 25: case 42: case 61:
		System.out.println("今年は貴方の本厄です。");
		break;
	case 24: case 41: case 60:
		System.out.println("今年は貴方の前厄です。");
		break;
	default:
		System.out.println("今年は貴方の厄年ではありません。");
		break;
		
	}
}
else if (sex == sexFemale) {
	switch (age) {
	case 19: case 33: case 37: case 61:
		System.out.println("今年は貴方の本厄です。");
		break;
	case 18: case 32: case 36: case 60:
		System.out.println("今年は貴方の前厄です。");
		break;
	default:
		System.out.println("今年は貴方の厄年ではありません。");
		break;	
}
	}
}
}
```
2. Solution 2: 次優解
```
package test;

import java.time.LocalDate;
import java.util.*;

public class Test {

//Definition
public static int[] femaleHonnyakuAges = new int[] {19,33,37,61};
public static int[] maleHonnyakuAges = new int[] {25,42,61};		
public static final int sexMale = 1;
public static final int sexFemale = 2;

public static void go() {

//initialization
String inputYear = null;
String inputSex = null;
int birthYear = 0;
int thisYear = 0;
int age = 0;
int sex = 0;

//Display input information and request user's input of birth year
Scanner input = new Scanner(System.in);
System.out.println("【厄年計算】");
System.out.println("貴方の生年（西暦４桁）を入力してください:");
inputYear = input.nextLine();


//casting, get current year, calculate age
birthYear = Integer.parseInt(inputYear);
thisYear = LocalDate.now().getYear();
age = thisYear - birthYear + 1;

//Display age information and request user's input of sexuality
System.out.println("貴方の年齢(数え年の年齢)は" + age + "歳です。");
System.out.println("");
System.out.println("貴方の性別を教えてください。（男性の場合1を、女性の場合2を入力してください。）");
inputSex = input.nextLine();

//casting, close input
sex = Integer.parseInt(inputSex);
input.close();


//Determination
if (isHonnyaku(sex, age)) {
	System.out.println("今年は貴方の本厄です。");
}
else if (isMaeyaku(sex, age)) {
	System.out.println("今年は貴方の前厄です。");
}
else {
	System.out.println("今年は貴方の厄年ではありません。");
}
}


public static boolean isHonnyaku(int sex, int age){
	boolean result = false;
	if (sex == sexMale) {
		for (int maleHonnyaku : maleHonnyakuAges) {
			if (maleHonnyaku == age) {
				result = true;
			}
		}
	}
	else if (sex == sexFemale) {
		for (int femaleHonnyaku : femaleHonnyakuAges) {
			if (femaleHonnyaku == age) {
				result = true;
			}
		}
	}
		else {
		}
	return result;
}

public static boolean isMaeyaku(int sex, int age) {
	boolean result = false;
	if (sex == sexMale) {
		for (int maleMaeyaku : maleHonnyakuAges) {
			if (maleMaeyaku == age + 1) {
				result = true;
			}
		}
	}
	else if (sex == sexFemale) {
		for (int femaleMaeyaku : femaleHonnyakuAges) {
			if (femaleMaeyaku == age + 1) {
				result = true;
			}
		}
	}
		else {
		}
	return result;

}
}

```
3. Solution 3: 最優解 將邏輯統整成methods
```
package test;

import java.time.LocalDate;
import java.util.*;

public class Test {

//Definition
public static final int[] FEMALEHONNYAKUAGES = new int[] {19,33,37,61};
public static final int[] MALEHONNYAKUAGES = new int[] {25,42,61};		
public static final int SEXMALE = 1;
public static final int SEXFEMALE = 2;

public static void go() {

//initialization
String inputYear = null;
String inputSex = null;
int birthYear = 0;
int thisYear = 0;
int age = 0;
int sex = 0;

//Display input information and request user's input of birth year
Scanner input = new Scanner(System.in);
System.out.println("貴方の生年（西暦４桁）を入力してください:");
inputYear = input.nextLine();


//casting, get current year, calculate age
birthYear = Integer.parseInt(inputYear);
thisYear = LocalDate.now().getYear();
age = thisYear - birthYear + 1;

//Display age information and request user's input of sexuality
System.out.println("貴方の年齢(数え年の年齢)は" + age + "歳です。");
System.out.println("");
System.out.println("貴方の性別を教えてください。（男性の場合1を、女性の場合2を入力してください。）");
inputSex = input.nextLine();

//casting, close input
sex = Integer.parseInt(inputSex);
input.close();


//Determination

if (isHonnyaku(sex, age)) {
	System.out.println("今年は貴方の本厄です。");
}
else if (isHonnyaku(sex, age + 1)) {
	System.out.println("今年は貴方の前厄です。");
}

else if (isHonnyaku(sex, age - 1)) {
	System.out.println("今年は貴方の後厄です。");
}

else {
	System.out.println("今年は貴方の厄年ではありません。");
}

}

public static boolean isHonnyaku(int sex, int age){
	int[] array = null;
	if (sex == SEXMALE) {
		array = MALEHONNYAKUAGES;
		return hasValue(age, array);
		
	}
	else if (sex == SEXFEMALE) {
		array = FEMALEHONNYAKUAGES;
		return hasValue(age, array);

	}
	return false;	


}
	
	
static boolean hasValue(int age, int[] array) {
	for (int x : MALEHONNYAKUAGES) {
		if(x == age) {
			return true;
			}
		}
	
	for (int y : FEMALEHONNYAKUAGES) {
		if(y == age) {
			return true;
			}
	}
	return false;
}
}

```