Skip to content

Instantly share code, notes, and snippets.

@s16003
Created November 20, 2017 07:09
Show Gist options
  • Save s16003/265426e5af379be8b8ae97c4099eb9d0 to your computer and use it in GitHub Desktop.
Save s16003/265426e5af379be8b8ae97c4099eb9d0 to your computer and use it in GitHub Desktop.
import java.util.*;
class testToOrdinal {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String n;
System.out.println("0以上の整数を入力: ");
n = sc.next();
if (Integer.parseInt(n) < 0) {
System.out.println("0以上の数を入力してください");
System.exit(0);
}
System.out.println(new ToOrdinal(n));
}
}
class ToOrdinal {
private String num;
ToOrdinal(String num) {
this.num = num;
}
@Override
public String toString() {
switch (num.charAt(num.length() - 1)) {
case '1':
return num + "st";
case '2':
return num + "nd";
case '3':
return num + "rd";
default:
return num + "th";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment