Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Project: Simple_Conditions - created by borkins on 2017-03-25.
- */
- import java.util.Scanner;
- public class _16c_Numbers_0_100_toText
- {
- public static void main(String[] args)
- {
- Scanner scan = new Scanner(System.in);
- int number = Integer.parseInt(scan.nextLine());
- int units = number % 10; // Get the digit in the units
- int tenths = number / 10; // Get the digit in the tenths
- // Using array of String elements
- /* Arrays are collection of elements of the same type
- * Each element has its own index (0, 1, 2, 3, ...).
- * Arrays are zero based, so the elements start from index 0.
- * Elements: [1, 2, 3, 4, 5, 6, 7, 8, 9]
- * Indexes: 0 1 2 3 4 5 6 7 8
- */
- // Declaring Array of numbers in text (0..19 by 1)
- /* Note that in numbers 10..19 the digit in the tenths is 1,
- and the numbers contains only one word (eleven, twelve..nineteen).
- That's why we can put them together with the units.
- */
- String[] unitsEng = {
- "zero", "one", "two", "three", "four", "five",
- "six", "seven", "eight", "nine", "ten",
- "eleven", "twelve", "thirteen", "fourteen", "fifteen",
- "sixteen", "seventeen", "eighteen", "nineteen"
- };
- // Declaring Array of numbers in text (20..90 by 10)
- /* Note: I left first two elements as empty strings.
- * That's because I need to call from third element,
- * which is at index 2.
- */
- String[] tenthsEng = {"", "",
- "twenty", "thirty", "forty", "fifty",
- "sixty", "seventy", "eighty", "ninety"
- };
- // If either conditions is true, print "invalid number".
- if (number < 0 || number > 100)
- {
- System.out.println("invalid number");
- }
- // In that point we are sure that the number is in the range 0..100.
- // If true, number is in range 0..19, print the result by the ternary logic
- // result = Condition ? Statement1 : Statement2;
- else if (number < 20)
- {
- // If in tenths is 0, print numbers 0..9, else print numbers 10..19.
- System.out.println((tenths == 0) ? unitsEng[units] : unitsEng[units + 10]);
- }
- // In that point number is in range 20..100
- // If true, number is in range 20..99, print the result by the ternary logic
- else if (number < 100)
- {
- // If digit in units is 0, print numbers 20, 30, 40..90,
- // else print the numbers with two words.
- System.out.println((units == 0) ? tenthsEng[tenths] : tenthsEng[tenths] + " " + unitsEng[units]);
- }
- // The number is 100, print 'one hundred'.
- else
- {
- System.out.println("one hundred");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement