이번에는 break문입니다. 순환문을 빠져나올때 사용합니다.


public static bool IsNumber(){

   bool isCheck = true;

for(int i=2;i<5;i++){

if(n % i == 0){                //짝수이면 isCheck를 false로 변환하고 순환문을 빠져나옴.

     isCheck = false;

break;                                         

}

}

return isCheck;

}





break는 가장 가까운 순환문을 빠져나와요.

C#에서 홀수 짝수 구하는 예제입니다.

제가 공부용으로 작성하는 글이니 강좌처럼 진행되지는 않습니다.



using System;

using System.Collectioons.Generic;

using System.Text;


namespace IfElseTest{

public class Test{

public static void Main(string[] args){

Console.Write("숫자를 입력하여 주세요:");

int n = int.Parse(console.ReadLine())); //콘솔에서 입력받은 값을 int로 형변환)


bool aaa = Number.IsCheck(n); //하단에 작성된 Number 클래스의 isCheck메서드 실행

if(aaa){

Console.WriteLine("{0}은 짝수",n);

}

else{

Console.WriteLine("{0}은 홀수",n);

}

}

}


public class Number{

public static bool IsCheck(int n){

bool check = true;   //bool형식은 true 아니면 false임

if(n%2 ==0){        

check=true;

}

else{

check = false;

}

return check;

}

}



}



+ Recent posts