
using System;
namespace Csharp
{
class Program
{
static void Main(string[] args)
{
int hp = 100;
bool isDead = (hp <= 0);
if (isDead)
{
Console.WriteLine("You are dead!");
}
}
}
}
switch (choice)
{
case 0:
Console.WriteLine("가위 입니다.");
break;
case 1:
Console.WriteLine("바위 입니다.");
break;
case 2:
Console.WriteLine("보 입니다.");
break;
case 3:
Console.WriteLine("치트키 입니다.");
break;
default:
Console.WriteLine("다 실패했습니다.");
break;
}
int number = 25;
bool isPair = ((number % 2) == 0 ? ture : false);
using System;
namespace Csharp
{
class Program
{
static void Main(string[] args)
{
// 0: 가위, 1: 바위, 2: 보
Random rand = new Random();
int aiChoice = rand.Next(0, 3); // 0 ~ 2 사이의 랜덤 값
Console.WriteLine("가위(0), 바위(1), 보(2) 중 하나를 선택하세요:");
int choice = Convert.ToInt32(Console.ReadLine());
switch (choice)
{
case 0:
Console.WriteLine("당신의 선택은 가위입니다.");
break;
case 1:
Console.WriteLine("당신의 선택은 바위입니다.");
break;
case 2:
Console.WriteLine("당신의 선택은 보입니다.");
break;
default:
Console.WriteLine("잘못된 선택입니다. 가위(0), 바위(1), 보(2) 중 하나를 선택하세요.");
return;
}
switch (aiChoice)
{
case 0:
Console.WriteLine("컴퓨터의 선택은 가위입니다.");
break;
case 1:
Console.WriteLine("컴퓨터의 선택은 바위입니다.");
break;
case 2:
Console.WriteLine("컴퓨터의 선택은 보입니다.");
break;
}
// 승부 결정
if (choice == aiChoice)
{
Console.WriteLine($"무승부입니다! 당신은 {GetHandName(choice)}를 선택하고, 컴퓨터는 {GetHandName(aiChoice)}를 선택했습니다.");
}
else if ((choice == 0 && aiChoice == 2) || (choice == 1 && aiChoice == 0) || (choice == 2 && aiChoice == 1))
{
Console.WriteLine($"승리입니다! 당신은 {GetHandName(choice)}를 선택하고, 컴퓨터는 {GetHandName(aiChoice)}를 선택했습니다.");
}
else
{
Console.WriteLine($"패배입니다! 당신은 {GetHandName(choice)}를 선택하고, 컴퓨터는 {GetHandName(aiChoice)}를 선택했습니다.");
}
}
static string GetHandName(int hand)
{
switch (hand)
{
case 0:
return "가위";
case 1:
return "바위";
case 2:
return "보";
default:
return "알 수 없음";
}
}
}
}
기존에 작성한 가위바위보 게임은 하드코딩이기 때문에 유지보수측면에서 어려움이 있다.
switch 문의 경우 상수 (고정된 변수)만 사용할 수 있다. 변동되는 값은 사용할 수 없다.
const int Rock = 0;
using System;
namespace Csharp
{
enum Choice
{
Rock = 1,
Paper = 2,
Scissors = 0
}
class Program
{
static void Main(string[] args)
{
(int)Choice.Scissors;
}
}
}
using System;
namespace Chasrp
{
class Program
{
static void Main(string[] args)
{
int cnt = 5;
while (cnt > 0)
{
Console.WriteLine("Hello World!");
cnt--;
}
}
}
}
while 문의 조건을 만족해야 while 문이 돌아간다. 즉, 거짓이 될 때까지 무한으로 돌아간다
do while 문 : 우선 실행 후 while문을 체크해서 한번 더 들어올지 체크한다.
using System;
namespace Chasrp
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 5; i++)
{
Console.WriteLine(i);
}
}
}
}
using System;
namespace Chasrp
{
class Program
{
static void HelloWorld()
{
Console.WriteLine("Hello World!");
}
static void Main(string[] args)
{
Program.HelloWorld();
}
}
}
using System;
namespace Csharp
{
class Program
{
static int Add(int a, int b)
{
int result = a + b;
return result;
}
static void Main(string[] args)
{
int result = Program.Add(4, 5);
Console.WriteLine(result);
}
}
}
using System;
namespace Csharp
{
class Program
{
static void AddOne(ref int number)
{
number++;
}
static void Main(string[] args)
{
int a = 0;
Program.AddOne(ref a);
Console.WriteLine(a);
}
}
}
실제 연산 등에서는 ref를 사용하지 않고 int 함수 등을 사용하여 값을 변환하는데 값을 swap 하는 등의 경우엔 메모리를 직접 참조하여 변경하는 경우가 생긴다
out을 사용해서 반환 하는 값을 여러 개를 반환할 수 있다. 대신 함수를 호출할 때 out을 사용한다는 것을 선언 해주어야 한다. (직접 메모리 값을 변환하기 때문)
using System;
namespace Csharp
{
class Program
{
static void Divide(int a, int b, out int result1, out int result2)
{
result1 = a / b;
result2 = a % b;
}
static void Main(string[] args)
{
int num1 = 10;
int num2 = 3;
int result1;
int result2;
Divide(num1, num2, out result1, out result2);
Console.WriteLine(result1);
Console.WriteLine(result2);
}
}
}
using System;
namespace Csharp
{
class Program
{
static int Add (int a, int b)
{
Console.WriteLine("Add int 호출");
return a + b;
}
static float Add (float a, float b)
{
Console.WriteLine("Add float 호출");
return a + b;
}
static void Main(string[] args)
{
int ret = Program.Add(1, 2);
Console.WriteLine(ret);
float ret2 = Program.Add(2.2f, 3.2f);
Console.WriteLine(ret2);
}
}
}