Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. fun ageDescription(age: Int): String {
  2. return when {
  3. age / 10 % 10 == 1 -> "$age лет"
  4. age % 10 == 1 -> "$age год"
  5. age / 10 % 10 == 9 -> "$age лет"
  6. else -> "$age года"
  7. }
  8. }
  9.  
  10. @Test
  11. fun ageDescription() {
  12. assertEquals("1 год", ageDescription(1))
  13. assertEquals("21 год", ageDescription(21))
  14. assertEquals("132 года", ageDescription(132))
  15. assertEquals("12 лет", ageDescription(12))
  16. assertEquals("111 лет", ageDescription(111))
  17. assertEquals("199 лет", ageDescription(199))
  18. }
  19.  
  20. public static void main(String[] args) {
  21. for (int i = 1; i <= 200; i++) {
  22. System.out.println(ageDescription(i));
  23. }
  24. }
  25.  
  26. private static String ageDescription(int age) {
  27. if (age < 1) return null;
  28. int lastDigit = age % 10;
  29. String ageStr = "";
  30. if (age - lastDigit == 10) {
  31. ageStr = " лет"; // 10 - 19 лет
  32. } else if (lastDigit == 0) {
  33. ageStr = " лет";
  34. } else if (lastDigit == 1) {
  35. ageStr = " год";
  36. } else if (lastDigit <= 4) {
  37. ageStr = " года";
  38. } else {
  39. ageStr = " лет";
  40. }
  41. return age + ageStr;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement