2014-09-23 2 views
-1

안녕하세요 저는 건네받은 두 개의 정수에서 1, 10, 수백 및 1000 자리에서 가장 작은 자릿수를 선택한 다음 각각에 대해 가장 작은 값으로 이루어진 int를 반환하는 메서드를 만들고 있습니다. 장소. 예를 들어 int a = 4321이고 int b = 1957이면 메서드는 1321을 반환합니다. 이것은 지금까지 제 코드이며 모든 것이 있다고 생각하지만 새로운 값을 정수로 올바르게 반환하는 방법을 찾을 수 없습니다.하나의 값으로 여러 ints 반환

public static int biggestLoser(int a, int b){ 
    int first; 
    int second; 
    int third; 
    int fourth; 
    if(a>9999 || a<1000 || b>9999 || b<1000){ 
     if(a>b) 
      return b; 
     else 
      return a; 
     } 
    else{ 
     if(a%10 < b%10) 
      first=a%10; 
     else 
      first=b%10; 
     if(a/1000<b/1000) 
      fourth=a/1000; 
     else 
      fourth=b/1000; 
     if(a/100%10<b/100%10) 
      second=a/100%10; 
     else 
      second=b/100%10; 
     if(a/10%10<b/10%10) 
      third=a/10%10; 
     else 
      third=b/10%10; 
     //int total=fourth,third,second,first;????? 
     //return total; 
    } 
} 
+2

를 배열의 각 요소를 얻을 수있는

(10)의 곱과 추가 고려 당신이 종이에서하는 것과 똑같습니다. 더 이상 복잡하지 않게 만들어야합니다. 기본 3 학년 산술입니다. –

답변

2

먼저 사소한 오류가 있습니다. secondthird 코드를 교환해야합니다. 를 고정 후

if(a/100%10<b/100%10) 
    third=a/100%10; 
else 
    third=b/100%10; 
if(a/10%10<b/10%10) 
    second=a/10%10; 
else 
    second=b/10%10; 

은 단순히 말 :

int total = first + 10 * second + 100 * third + 1000 * fourth; 
return total; 

그리고 바로 그거야.

+0

S % * @ 나는 그 사람을 알아 차리지 못했다! – nebularis

0

먼저 문자열을 형성하고 Integer.parseInt()을하거나 int total = (first*1)+(second*10)+(third*100)+(fourth*1000); 같은 몇 가지 계산을 할 수 있습니다.

+0

감사합니다. – nebularis

0

이 같은 것을 구현하기 위해 더 나은 것 : 모든 코드의

int returnValue = 0; 
for(int digit = 0; digit < 4; digit++) { 
    int aDigit = (a/Math.pow(10, digit)) % 10; // Gets the digit 
    int bDigit = (b/Math.pow(10, digit)) % 10; 
    int max = (a > b)? a : b; // Calculates de maximum digit 
    returnValue += max * Math.pow(10, digit); //Adds the new digit to the return value 
} 
return returnValue; 
+1

Math.pow는 매우 비효율적이지만 단순히 정수를 곱해야합니다. –

+0

감사하지만 약간 너무 복잡합니다! – nebularis

0
int i=1000,z=0; 
    do{ 
    int x = a/i; 
    a=a%i; 
    int y = b/i; 
    b=b%i; 
    if(x<y) 
    z+=x*i; 
    else z+=y*i; 
    i=i/10; 
}while(i>0); 
return z; 
//this is just logic, works for 4 digit numbers 
0

단지 값을 int 배열로 반환합니다. 내가이 일을 당신의 방법을 리팩토링 한

public static int[] biggestLoser(int a, int b){ 
int first; 
int second; 
int third; 
int fourth; 
int [] values; 
if(a>9999 || a<1000 || b>9999 || b<1000){ 
    if(a>b) 
     return b; 
    else 
     return a; 
    } 
values = new int[4]; 
else{ 
    if(a%10 < b%10) 
     first=a%10; 
    else 
     first=b%10; 
    if(a/1000<b/1000) 
     fourth=a/1000; 
    else 
     fourth=b/1000; 
    if(a/100%10<b/100%10) 
     second=a/100%10; 
    else 
     second=b/100%10; 
    if(a/10%10<b/10%10) 
     third=a/10%10; 
    else 
     third=b/10%10; 
    //int total=fourth,third,second,first;????? 
    values[0] = first; 
    values[1] = second; 
    values[2] = third; 
    values[3] = fourth; 
    return value; 
} 

} 기본 방법 당신은 지금 루프

의를 통해
for(int x : values){ 
    System.out.print(x); 
} 
관련 문제