Server/Java
JAVA ChoiceFormat
selfstarter
2020. 1. 7. 20:56
JAVA ChoiceFormat
- ChoiceFormat으로 숫자 범위에 따라 다른 format으로 처리할 수 있다
- 첫번째 방법은 if문으로 점수에 따라 등급을 주는 예제이다
- 숫자범위로 걸러지므로 낮은숫자-높은숫자 순으로 작성해야한다
- 두번째 방법은 #는 같다. |는 또는. <,> 부호를 사용해서 숫자에 따른 format을 지정할 수 있다
- 어디에도 숫자가 속하지 않는다면 맨 마지막 format이 선택된다
- [example] (http://cris.joongbu.ac.kr/course/java/api/java/text/ChoiceFormat.html)
import java.text.ChoiceFormat;
public class Main {
public static void main(String[] args) {
// first
double[] score = {60, 70, 80, 90};
//double[] score = {90, 80, 70, 60};
String[] grade = {"D", "C", "B", "A"};
ChoiceFormat choiceFormat = new ChoiceFormat(score, grade);
int[] scoreList = {79, 90, 20, 44, 80, 94, 70, 80, 61};
for (int i = 0; i < scoreList.length; ++i) {
System.out.println(scoreList[i] +":"+choiceFormat.format(scoreList[i]));
}
// second
ChoiceFormat choiceFormat2 = new ChoiceFormat("0#is zero | 1#is one | 1<is else");
System.out.println("Formatter Pattern : " + choiceFormat2.toPattern());
System.out.println(choiceFormat2.format(0));
System.out.println(choiceFormat2.format(1));
System.out.println(choiceFormat2.format(2));
System.out.println(choiceFormat2.format(2.323123));
//System.out.println(choiceFormat2.format("2")); // 숫자범위가 아니라면 exception 남
//System.out.println(choiceFormat2.format("가나"));
}
}
```