Created
November 20, 2017 07:09
-
-
Save s16003/265426e5af379be8b8ae97c4099eb9d0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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