[re]두 숫자의 각 자리수를 비교하기


[ 다음 글들 ] [ 이어서 글올리기(답하기) ] [ 자바 묻고 답하기 ]

글쓴이 :최기섭 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);
}
}


다음 글들:



이어서 글올리기(답하기)

이름:
E-Mail:
제목:
내용:
HTML 태그 포함 여부: HTML 문서일 경우 체크
관련 URL(선택):
URL 제목(선택):
관련 이미지 URL:


[ 다음 글들 ] [ 이어서 글올리기(답하기) ] [ 자바 묻고 답하기 ]