2016-11-20 7 views
1

작업에 문제가 있습니다. 다음 doWhile 루프를 While 루프로 변형시켜 두 루프의 결과가 동일해야합니까? Integer 유형은 doWhile 루프에 있습니다. 하지만 int 유형의 차이점은 무엇입니까? 이 작업을 어떻게 해결합니까?While 루프에서 doWhile 변환

doWhile 루프 :

public Integer a_doWhile(int x){ 
    int i = 0; 
    Integer result; 
    do { 
     result = ++i*i; 
    } while (result < x); 
    return result; 
} 

내 솔루션 :

public Integer a_while(int x){ 
    int i=0; 
    int result; 
    while(result < x){ 
     result = ++i*i; 
    return result; 
    } 
} 

하지만 내 솔루션은 잘못된 것입니다. 이 "Integer"로 sth을 수행해야합니다. 누군가 제발 나를 도와 줄 수 있습니까? 고마워요 :)

답변

0

int와 정수는 같은 것의 표현이 다릅니다. Integer가 객체 표현 인 동안 Int는 원시적입니다. 이것은 당신이

public Integer a_While(int x){ 
    int i = 0; 
    Integer result = ++i*i; 
    while (result < x) { 
     result = ++i*i; 
    } 
    return result; 
+0

처럼 마법처럼 일했다 반면에 dowhile을 변환 할 수 있습니다 여기에 What is the difference between an int and an Integer in Java and C#?

설명합니다. 리드 발을 주셔서 감사합니다. –

+0

답변으로 표시하는 것을 잊지 마세요. – Leadfoot