변경이력

돌아가기
2 8개 문자 추가 8개 문자 삭제

2016/10/12 08:20

궁금이

```{.java} package codingdojang; public class DashInsert { enum NUM { EVEN, ODD, NOT } private static String dashInsert(String num) { StringBuffer result = new StringBuffer(); for ( int i = 0; i < num.length() - 1; i++ ) { int firstDigit = Integer.parseInt( num.charAt( i ) + "" ); int secondDigit = Integer.parseInt( num.charAt( i + 1 ) + "" ); result.append( firstDigit ); switch ( isDashInsertDash( firstDigit, secondDigit) ) { case EVEN: result.append( "*" ); break; case ODD: result.append( "-" ); break; default: break; } } result.append( num.charAt( num.length()-1 ) ); return result.toString(); } private static NUM isDashInsertDash( int firstDigit, int secondDigit ) { if ( isEven( firstDigit ) && isEven( secondDigit ) ) { return NUM.EVEN; } else if ( !isEven( firstDigit) && !isEven( secondDigit )) { return NUM.ODD; } else { return NUM.NOT; } } private static boolean isEven( int firstDigit ) { return firstDigit % 2 == 0; } public static void main( String[] args ) { String num = "4546793"; String result = dashInsert( num ); System.out.println( result ); } } ```
```{.java} package codingdojang; public class DashInsert { enum NUM { EVEN, ODD, NOT } private static String dashInsert(String num) { StringBuffer result = new StringBuffer(); for ( int i = 0; i < num.length() - 1; i++ ) { int firstDigit = Integer.parseInt( num.charAt( i ) + "" ); int secondDigit = Integer.parseInt( num.charAt( i + 1 ) + "" ); result.append( firstDigit ); switch ( isDashInsertDash( firstDigit, secondDigit) ) { case EVEN: result.append( "*" ); break; case ODD: result.append( "-" ); break; default: break; } } result.append( num.charAt( num.length()-1 ) ); return result.toString(); } private static NUM isDashInsertDash( int firstDigit, int secondDigit ) { if ( isEven( firstDigit ) && isEven( secondDigit ) ) { return NUM.EVEN; } else if ( !isEven( firstDigit) && !isEven( secondDigit )) { return NUM.ODD; } else { return NUM.NOT; } } private static boolean isEven( int firstDigit ) { return firstDigit % 2 == 0; } public static void main( String[] args ) { String num = "4546793"; String result = dashInsert( num ); System.out.println( result ); } } ```
1 Original

2016/10/12 08:17

궁금이

코딩도장

코딩도장은 프로그래밍 문제풀이를 통해서 코딩 실력을 수련(Practice)하는 곳입니다.