2012-09-18 4 views
1

s2이 null 인 경우 디버거가 모든 복잡한 문자열 조작을 건너 뛰고 첫 번째 if 블록에 표시된대로 s1 + s2 + s3 대신 null을 반환하도록 논리를 만들려고합니다. 내가 어딘가 틀렸어?Java 함수에서 continue 문

public static String helloWorld(String s1, String s2, String s3){ 
    if(s2==null){ 
    continue; 
    return null; 
    } 

    ... lots of string manipulation involving s1, s2 and s3. 

    return (s1+s2+s3); 
} 
+2

당신은'CONTINUE '를 사용하는 루프가 필요합니다. –

+0

@bouncingHippo : null 문자열 만 건너 뛰는 것에 관심이 있습니까? 아니면 s2가 빈 문자열 (""- this가 null이 아닙니다) 인 경우 null을 반환하고 싶습니까 – tehdoommarine

+0

s2 == null이 다른 경우 null을 반환하고 싶습니다. s2 = "" – bouncingHippo

답변

6

는 수행이 계속 사용

for(Foo foo : foolist){ 
    if (foo==null){ 
     continue;// with this the "for loop" will skip, and get the next element in the 
       // list, in other words, it will execute the next loop, 
       //ignoring the rest of the current loop 
    } 
    foo.dosomething(); 
    foo.dosomethingElse(); 
} 

처럼, 루프입니다 계속하지 않습니다

public static String helloWorld(String s1, String s2, String s3){ 
    if(s2==null){ 
    return null; 
    } 

    ... lots of string manipulation involving s1, s2 and s3. 

    return (s1+s2+s3); 
} 
+0

일 때 솔루션에 대해 s2 == null이면 null을 반환하고 (s1 + s2 + s3)을 반환하지 않습니까? – bouncingHippo

+0

아마 테스트했을 것입니다. 그렇습니다. –

2

continue 문 루프 (for, while, do-while에 사용됩니다), if 진술 문은 해당되지 않습니다.

귀하의 코드는 당신이 continue 필요가 없습니다

public static String helloWorld(String s1, String s2, String s3){ 
    if(s2==null){ 
    return null; 
    } 

    ... lots of string manipulation involving s1, s2 and s3. 

    return (s1+s2+s3); 
} 
2

해야한다, return null;은 충분하다.

continue은 루프가 나머지 블록을 건너 뛰고 다음 단계를 계속 진행하기를 원할 때 루프 내에서 사용됩니다.

예 :

for(int i = 0; i < 5; i++) { 
    if (i == 2) { 
     continue; 
    } 

    System.out.print(i + ","); 
} 

인쇄됩니다

0,1,3,4,