Stuck on getting the prompt to enter a number 1 to 10 - and results should add the st, rd, th, and nd to the number. Write a program that prompts the user to enter any integer from 1 to 10 and then displays the integer in ordinal form with suffix attached.
public class Ordinals {
public static String Ordinals(int i) {
System.out.println("Enter an integer between 0 to 10: ");
Scanner input = new Scanner(System.in);
int hundred = value % 100;
int tens = value % 10;
if (hundred - tens == 10) {
return "th";
}
switch (tens) {
case 1:
return "st";
case 2:
return "nd";
case 3:
return "rd";
default:
return "th";
}
public static void main(String[] args) {
Ordinals number = new Ordinals();
for (int i = 1; i <= 10; i++) {
String st = number.Ordinals(i);
System.out.println(i + " = " + i + st);
}
}
}
In my opinion it would be better to move Scanner
to main:
public static void main(String[] args) {
Ordinals number = new Ordinals();
Scanner input = new Scanner(System.in);
while(true){
System.out.println("Enter an integer between 0 to 10: ");
int i = input.nextInt();
System.out.println(i+number.Ordinals(i));
}
}
and then add to Ordinals:
int value = i;
Method will be independent form input type.