Reputation: 33
I am coding in Java Main. I am writing a program that will count the resources gained from a daily rewards in a game. I have declared an Int variable for every month that holds the value for the number of days in that month. Each variable includes the number of days for itself, and all previous months, as so:
int month;
int january = 31;
int february = 28; 59
int march = 31; 90
int april = 30; 120
int may = 31; 151
int june = 30; 181
int july = 31; 212
int august = 31; 243
int september = 30; 273
int october = 31; 304
int november = 30; 334
int december = 31; 365
the number that follows is what will be in the statement, i just have not put them in so i can keep the #of days as reference. Anyway, what I want to do is have the user input the current month and the month they want to know how many resources they will have, saved as String variables currentMonth and laterMonth respectively. What I want to do is check to see if the String variables (say, September and March) have the same name as their int var counterparts, and if so, subtract int march from int september (273 - 90) to get the number of days in between to get int month. Is it possible for there to be such a check? Maybe I need to set up an array? I appreciate your help!
Upvotes: 1
Views: 78
Reputation: 171
I think the problem can be solved using either a HashMap or two arrays. The two arrays approach is a bit easier in my opinion:
static String[] monthNames = new String[]{
"january",
"february",
"march",
"april",
"may",
"june",
"july",
"august",
"september",
"october",
"november",
"december"
};
public static void main(String[] args) {
int[] numOfDays = new int[]{31,28,31,30,31,30,31,31,30,31,30,31};
int mo2 = getConsecutiveMonth("september");
int mo1 = getConsecutiveMonth("march");
int septemberMarch = sumBetween(numOfDays, mo1, mo2);
System.out.println(septemberMarch);
}
public static int getConsecutiveMonth(String m){
String lowercaseM = m.toLowerCase();
for(int i = 0; i<monthNames.length; i++){
if(monthNames[i].equals(lowercaseM))
return i;
}
return -1;
}
public static int sumBetween(int[] a, int i, int j){
int sum = 0;
for (int idx = i; idx < j; idx++) {
sum += a[idx];
}
return sum;
}
Upvotes: 1
Reputation: 79425
You do not need to maintain an array for it. The java.time
API provides you with a lot of options which you can leverage to achieve what you need. Given below is an example:
import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.ChronoUnit;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println(Month.FEBRUARY.maxLength());
// Difference between 31st Mar and 30th Sep
System.out.println(
Math.abs(ChronoUnit.DAYS.between(LocalDate.of(2020, Month.SEPTEMBER, Month.SEPTEMBER.maxLength()),
LocalDate.of(2020, Month.MARCH, Month.MARCH.maxLength()))));
// ############## Interactive ##############
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first month[1-12]: ");
int first = scanner.nextInt();
System.out.print("Enter the second month[1-12]: ");
int second = scanner.nextInt();
System.out.println(Math.abs(ChronoUnit.DAYS.between(LocalDate.of(2020, first, Month.of(first).maxLength()),
LocalDate.of(2020, second, Month.of(second).maxLength()))));
}
}
A sample run:
29
183
Enter the first month[1-12]: 3
Enter the second month[1-12]: 9
183
Upvotes: 2