2014-01-21 3 views
0

두 개의 큰 숫자 (각 숫자가 다른 문자 인 두 개의 배열로)를 추가하는 메서드를 만들어야합니다. 이미 코드를 만들었지 만 제대로 작동하지 않습니다. 누군가 나를 도우 려하고 그것을 조사 할 수 있었습니까?두 개의 큰 숫자를 두 개의 배열로 추가하기

public BigNumber add(BigNumber number2){ 
    BigNumber x = null; 
    char[] m = null; 
    long y = 0; 
    boolean tmpBool = false; 
    boolean leftIsLonger = false; 

    if (this.n.length >= number2.n.length){ 
     m = new char[this.n.length + 1]; 
     y = number2.n.length; 
     leftIsLonger = true; 
    }else{ 
     m = new char[this.n.length + 1]; 
     y = this.n.length; 
    } 

    int i; 
    for (i = 0; i < y; i++){ 
     char[] tmp1 = new char[1]; 
     this.number.getChars(i, i, tmp1, 1); 
     int left = Character.getNumericValue(tmp1[0]); 

    int j; 
    for (j = 0; j < y; j++){ 
     char[] tmp2 = new char[1]; 
     this.number.getChars(i, i, tmp2, 1); 
     int right = Character.getNumericValue(tmp2[0]); 

     int z = left + right; 

     if (tmpBool){ 
      z++; 
      tmpBool = false; 
     } 

     if (z > 9){ 
      tmpBool = true; 
      z = z%10; 
     } 

     m[i]= (char) z; 
    }} 

    for (int k = i; k < m.length - 1; k--){ 
     if (leftIsLonger){ 
      if (tmpBool){ 
       int c = Character.getNumericValue(this.n[k]); 
       if (c > 9){ 
        tmpBool = true; 
        c = c%10; 
        m[k] = (char) (c); 
       }else{ 
        tmpBool = false; 
        m[k] = (char) (c + 1); 
       } 
      }else 
       m[k] = this.n[k]; 
     }else{ 
      if (tmpBool){ 
       int c = Character.getNumericValue(number2.n[k]); 
       if (c > 9){ 
        tmpBool = true; 
        c = c%10; 
        m[k] = (char) (c); 
       }else{ 
        tmpBool = false; 
        m[k] = (char) (c + 1); 
       } 
      }else 
       m[k] = this.n[k]; 
     } 
    } 

    return x; 
} 
+2

코드가 작동하지 않는 이유를 설명해야합니까? 너는 뭘하고있어? –

+0

코드에 무엇이 잘못되었는지 자세히 설명해주십시오. 루프 및 조건을 상상해 보는 것은 매우 어렵습니다. –

+1

코드 검토를 위해 [CodeReview] (http://codereview.stackexchange.com/)가 있습니다. :) – Fildor

답변

2

처음에는 BigNumber x = null을 작성하고 최종적으로 반환합니다. 그 사이에, 나는 그것이 설정된 곳을 찾을 수없는 것 같습니까? 그래서 전체 기능

당신이 무엇을
public BigNumber add(BigNumber number2){ 
    return null; 
} 

에 "최적화"그게 항상 null을 반환하는 이유입니다.

관련 문제