fork(6) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. System.out.print(fixNumerical(342, "книга", "книги", "книг"));
  13. }
  14.  
  15. public static String fixNumerical (int num, String... arr)
  16. {
  17. Integer preLastDigit = num % 100 / 10;
  18. if (preLastDigit == 1) {
  19. return String.format("%d %s", num, arr[2]);
  20. }
  21.  
  22. Integer lastDigit = num % 10;
  23. switch (lastDigit) {
  24. case 1:
  25. return String.format("%d %s", num, arr[0]);
  26. case 2:
  27. case 3:
  28. case 4:
  29. return String.format("%d %s", num, arr[1]);
  30. default:
  31. return String.format("%d %s", num, arr[2]);
  32. }
  33. }
  34.  
  35. }
Success #stdin #stdout 0.03s 4386816KB
stdin
Standard input is empty
stdout
342 книги