Different Suffixes

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

Answer:

  • 1   st
  • 2   nd
  • 3   rd
  • 4 up to 20  th

Different Suffixes

The first integer is prompted for outside of the loop. Inside the loop body, it would be nice to use the following prompts:

Enter the 2nd integer (enter 0 to quit):
Enter the 3rd integer (enter 0 to quit):
Enter the 4th integer (enter 0 to quit):
Enter the 5th integer (enter 0 to quit):
Enter the 6th integer (enter 0 to quit):
.... and so on ....
Enter the 20th integer (enter 0 to quit):
Enter the 21th integer (enter 0 to quit):

The prompt shows poor grammar for integers 21, 22, 23, 31, 32, 33 and so on. Let us regard this as acceptable.

The output statement is now:

System.out.println( "Enter the " + 
  (count+1) + suffix + " integer (enter 0 to quit):" );

The variable suffix will be a reference to one of the Strings: "nd", "rd", or "th".

QUESTION 8:

A choice must be made from among three things. Can a single if-else statement do this?