본문 바로가기

.NET/C# Basic

흐름 제어 예제 기본문법 if for while swich foreach break continue

반응형
using System;

class GenderTest
{
public static void Main()
{
Console.WriteLine("아유보이? (y)/(n)");
char gender = (char)Console.Read();
if( gender == 'y')
Console.WriteLine("남자이시군요");
else if (gender == 'n')
Console.WriteLine("여자이시군요");
else 
Console.WriteLine("잘못된 성별을 입력하셨습니다");
}
}




using System;

class cyon

{
public static void Main()
{
Console.WriteLine("요일을 입력하세요");
char day = (char)Console.Read();
switch (day)
{
case '월' :
Console.WriteLine("월");
break;
case '화':
Console.WriteLine("화");
break;
case '수' :
Console.WriteLine("수");
break;
case '목' :
Console.WriteLine("목");
break;
case '금' :
Console.WriteLine("금");
break;
case '토' :
Console.WriteLine("토");
break;
case '일' :
Console.WriteLine("알");
break;
default:
Console.WriteLine("아닌데");
break;
}
}
}



using System;

class whileTest
{
public static void Main()
{
bool ugiy = false;
bool fool = false;
Console.WriteLine("정신 교정 프로그램");
while (!(ugiy && fool))
{
Console.WriteLine("당신은 잘생겼습니까? y/n");
if(Console.ReadLine() == "n")
ugiy = true;
else 
ugiy = false;
Console.WriteLine("당신은 똑똑합니까? y/n");
if(Console.ReadLine () == "n")
fool= true;
else
fool = false;
}
Console.WriteLine("축하합니다 당신은 정상입니다");
}
}


class whileTest
{
public static void Main()
{
bool ugly = false;
bool fool = false;

Console.WriteLine("******* 정신 교정 프로그램 ******");

do
{

Console.WriteLine("당신은 잘생겼습니까?(y/n)");
if (Console.ReadLine() == "n")
ugly = true;
else
ugly = false;

Console.WriteLine("당신은 똑똑합니까?(y/n)");
if (Console.ReadLine() == "n")
fool = true;
else
fool = false;
} while (!(ugly && fool));

Console.WriteLine("*** 축하드립니다. 당신은 이제 정상입니다. ***");
}
}






using System;

class forfortest
{
public static void Main()
{
for(int i=0; i<5; i++)
{
for(int j =0; j <= i ; j++)
{
Console.Write("*,");
}
Console.WriteLine();
}
}
}

using System;

class forfortest
{
public static void Main()
{
for(int i=0; i<5; i++)
{
for(int j =0; j <= i ; j++)
{
Console.Write("*,");
}
Console.WriteLine();
}
}
}


using System;

class foreachtest
{
public static void Main()
{
int[] Values = {12,43,64,56,32};
foreach(int value in Values)
{
Console.WriteLine(value);
}
}
}




using System;

class breakTest
{
public static void Main()
{
int[] Values = {12,43,54,56,32};
foreach(int aa in Values)
{
if(aa == 43)
break;
Console.WriteLine(aa);
}
}
}



using System;

class continueTest
{
public static void Main()
{
int[] args ={12,43,56,37};
foreach(int bb in args)
{
if(bb == 43) //43은 그냥 넘어가고 걔속 순환
continue;
Console.WriteLine(bb);
}
}
}

using System;

class gototest
{
public static void Main()
{
int[] vv = {12,34,56,678,45};
foreach(int a in vv)
{
if(a > 50)
goto tooBig;
Console.WriteLine("{0}?{1}", a,vv);
}
tooBig:
Console.WriteLine("Too Big!");
}
}