Javaで年と月の差を計算するので、どのように私は月からなる2つの日付のユーザーと年を促し、これら2つの日付の間の年数 と ヶ月を表示できるようにする必要があり
これは、入力、次のようになります、------------------------------、1999年4月
2002年6月
これは出力です:------- 3年2ヶ月
これは私が得る必要があるものです、 2年10か月間
これは私のコードがあるところですが、どんな助けにも感謝しています。
public class AgeDifference{
public static void main(String[] args){
//Scanner input = new Scanner(System.in);
String month1 = "June";
String month2 = "April";
int year1 = 1999;
int year2 = 2002;
int firstMonthNumber = 0;
int secondMonthNumber = 0;
int totalYear = 0;
int totalMonth = 0;
//input:
System.out.println("For the first date,");
System.out.print("Enter month: ");
//month1 = input.next();
System.out.print("Enter year: ");
//year1 = input.nextInt();
System.out.println("");
System.out.println("For the second date,");
System.out.print("Enter month: ");
//month2 = input.next();
System.out.print("Enter year: ");
//year2 = input.nextInt();
System.out.println("");
//processing:
if (month1.equalsIgnoreCase("January")){
firstMonthNumber = 1;
} else if(month1.equalsIgnoreCase("February")){
firstMonthNumber = 2;
} else if (month1.equalsIgnoreCase("March")){
firstMonthNumber = 3;
} else if(month1.equalsIgnoreCase("April")){
firstMonthNumber = 4;
} else if (month1.equalsIgnoreCase("May")){
firstMonthNumber = 5;
} else if(month1.equalsIgnoreCase("June")){
firstMonthNumber = 6;
} else if (month1.equalsIgnoreCase("July")){
firstMonthNumber = 7;
} else if(month1.equalsIgnoreCase("August")){
firstMonthNumber = 8;
} else if (month1.equalsIgnoreCase("September")){
firstMonthNumber = 9;
} else if(month1.equalsIgnoreCase("October")){
firstMonthNumber = 10;
} else if (month1.equalsIgnoreCase("November")){
firstMonthNumber = 11;
} else if(month1.equalsIgnoreCase("December")){
firstMonthNumber = 12;
}
if (month2.equalsIgnoreCase("January")){
secondMonthNumber = 1;
} else if(month2.equalsIgnoreCase("February")){
secondMonthNumber = 2;
} else if (month2.equalsIgnoreCase("March")){
secondMonthNumber = 3;
} else if(month2.equalsIgnoreCase("April")){
secondMonthNumber = 4;
} else if (month2.equalsIgnoreCase("May")){
secondMonthNumber = 5;
} else if(month2.equalsIgnoreCase("June")){
secondMonthNumber = 6;
} else if (month2.equalsIgnoreCase("July")){
secondMonthNumber = 7;
} else if(month2.equalsIgnoreCase("August")){
secondMonthNumber = 8;
} else if (month2.equalsIgnoreCase("September")){
secondMonthNumber = 9;
} else if(month2.equalsIgnoreCase("October")){
secondMonthNumber = 10;
} else if (month2.equalsIgnoreCase("November")){
secondMonthNumber = 11;
} else if(month2.equalsIgnoreCase("December")){
secondMonthNumber = 12;
}
totalYear = year1 - year2;
totalMonth = firstMonthNumber - secondMonthNumber;
リンクを貼り付けないでください。ここにコードを貼り付けます。 – progyammer