글쓴이 :최기섭 2000년 4월 24일 17:04:51
In Reply to: 두 숫자의 각 자리수를 비교하기 posted by 현정 on 2000년 4월 24일 02:43:06:
질문하신 내용을 처리하는 함수를 만들어 봤습니다.
배열로 만들기보다는 char String.charAt(int)를 사용하는게 나을 듯 싶습니다.
public void compare(String one, String another) {
int oneLength = one.length();
int anotherLength = another.length();
// 두 값의 길이가 다르면 긴 쪽의 앞엣 것을 자른다.
if ( oneLength > anotherLength )
one = one.substring( oneLength-anotherLength, oneLength);
else
another = another.substring( anotherLength-oneLength, anotherLength);
String result = null;
for ( int i=0; i<one.length(); i++ ) {
if ( one.charAt( i) == another.charAt( i) )
result = "같다.";
else
result = "다르다.";
System.out.println( (i+1) + "번째자리(" + one.charAt( i) + ", " +
another.charAt( i) + ")는 " +
result);
}
}