Created
September 21, 2016 09:30
-
-
Save tipochka/7c99c052ca3287dda05c0e0c094ebe92 to your computer and use it in GitHub Desktop.
Блинов. Глава 2. Вариант B. 4. Ввести число от 1 до 12. Вывести на консоль название месяца, соответствующего данному числу. Осуществить проверку корректности ввода чисел
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package oop.lesson1.homework; | |
import java.text.DateFormatSymbols; | |
import java.util.Scanner; | |
public class NumberMonth { | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
if(!scanner.hasNextInt()) { | |
throw new IllegalArgumentException("Argument not integer"); | |
} | |
System.out.println(getMonthName(scanner.nextInt())); | |
} | |
public static String getMonthName(int monthNumber) { | |
if (!hasMonth(monthNumber)) { | |
throw new IllegalArgumentException("Incorrect month number"); | |
} | |
return new DateFormatSymbols().getMonths()[monthNumber-1]; | |
} | |
public static boolean hasMonth(int monthNumber) { | |
if (monthNumber>= 1 && monthNumber <= 12) { | |
return true; | |
}else { | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment