This is the task..............
BirthMonth
This tutorial will use arrays and methods.
Using the material presented in lectures, implement and fully test the BirthMonth program. Use methods to implement at least the following functions:
• input and validate months
• output table of results
• find and output most frequent month
Problems are;
1 = Infinite loop when entering month number
2 = -1 needs to exit program with the results
Thanks for looking
import java.util.*; public class BirthMonth { public static void main(String[] args) { int[] MonthTally = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; inputMonths(MonthTally); outputMonths(MonthTally); findMost(MonthTally); } //select month born public static void inputMonths(int[] MonthTally) { System.out.println("Enter the number of the month you were born "); Scanner myKeyboard = new Scanner(System.in); int Month = myKeyboard.nextInt() - 1; while (Month != -1) { if (Month >= 13) { System.out.println("Enter a number between 1 - 12"); System.out.println("Enter the number of the month you were born "); Month = myKeyboard.nextInt() - 1; } } if (Month >= 1 && Month <= 12) { MonthTally[Month] = MonthTally[Month] + 1; } } //add up month total into array public static void outputMonths(int[] MonthTally) { for (int i = 0; i < MonthTally.length; i++) { System.out.println("Tally: " + i + " has the value " + MonthTally[i]); System.out.println("Tally value " + MonthTally[i]); } } //find highest month public static void findMost(int[] MonthTally) { int largestPos = 0; for (int i = 1; i <= MonthTally.length - 1; i++) { if (MonthTally[i] > MonthTally[largestPos]) { largestPos = i; } } //output results System.out.println("The most Common Month for pregnancy is: " + largestPos); largestPos = largestPos - 1; String[] monthName = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; System.out.print("This is the month of " + monthName[largestPos]); } }