Advertisement
borkins

16c. Numbers 0..100 to Text

Mar 24th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.91 KB | None | 0 0
  1. /**
  2.  * Project: Simple_Conditions - created by borkins on 2017-03-25.
  3.  */
  4.  
  5. import java.util.Scanner;
  6.  
  7. public class _16c_Numbers_0_100_toText
  8. {
  9.     public static void main(String[] args)
  10.     {
  11.         Scanner scan = new Scanner(System.in);
  12.        
  13.         int number = Integer.parseInt(scan.nextLine());
  14.         int units = number % 10;    // Get the digit in the units
  15.         int tenths = number / 10;    // Get the digit in the tenths
  16.        
  17.         // Using array of String elements
  18.         /* Arrays are collection of elements of the same type
  19.          * Each element has its own index (0, 1, 2, 3, ...).
  20.          * Arrays are zero based, so the elements start from index 0.
  21.          * Elements: [1, 2, 3, 4, 5, 6, 7, 8, 9]
  22.          * Indexes:   0  1  2  3  4  5  6  7  8
  23.          */
  24.        
  25.         // Declaring Array of numbers in text (0..19 by 1)
  26.         /* Note that in numbers 10..19 the digit in the tenths is 1,
  27.           and the numbers contains only one word (eleven, twelve..nineteen).
  28.           That's why we can put them together with the units.
  29.          */
  30.         String[] unitsEng = {
  31.                 "zero", "one", "two", "three", "four", "five",
  32.                 "six", "seven", "eight", "nine", "ten",
  33.                 "eleven", "twelve", "thirteen", "fourteen", "fifteen",
  34.                 "sixteen", "seventeen", "eighteen", "nineteen"
  35.         };
  36.        
  37.         // Declaring Array of numbers in text (20..90 by 10)
  38.         /* Note: I left first two elements as empty strings.
  39.          * That's because I need to call from third element,
  40.          * which is at index 2.
  41.          */
  42.         String[] tenthsEng = {"", "",
  43.                               "twenty", "thirty", "forty", "fifty",
  44.                               "sixty", "seventy", "eighty", "ninety"
  45.         };
  46.        
  47.         // If either conditions is true, print "invalid number".
  48.         if (number < 0 || number > 100)
  49.         {
  50.             System.out.println("invalid number");
  51.         }
  52.         // In that point we are sure that the number is in the range 0..100.
  53.         // If true, number is in range 0..19, print the result by the ternary logic
  54.         // result = Condition ? Statement1 : Statement2;
  55.         else if (number < 20)
  56.         {
  57.             // If in tenths is 0, print numbers 0..9, else print numbers 10..19.
  58.             System.out.println((tenths == 0) ? unitsEng[units] : unitsEng[units + 10]);
  59.         }
  60.         // In that point number is in range 20..100
  61.         // If true, number is in range 20..99, print the result by the ternary logic
  62.         else if (number < 100)
  63.         {
  64.             // If digit in units is 0, print numbers 20, 30, 40..90,
  65.             // else print the numbers with two words.
  66.             System.out.println((units == 0) ? tenthsEng[tenths] : tenthsEng[tenths] + " " + unitsEng[units]);
  67.         }
  68.         // The number is 100, print 'one hundred'.
  69.         else
  70.         {
  71.             System.out.println("one hundred");
  72.         }
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement