[TIL] C# 기초 - day1 - 2

뭉크의 개발·2023년 7월 19일
0

C# - Pre-Camp

목록 보기
2/7
post-thumbnail

들어가기 전

C#에서도 sout와 같은 단축키가 없나 해서 찾아봤다.

Console.WriteLine(); // cw + tab하면 바로 나온다!

연산자 = 산술 연산

요약

기호설명
+더하기
-빼기
*곱하기
/나누기
%나머지
++증가
--감소

더하기 연산

int x = 10;
int result;

result = x + 3;

빼기 연산

int x = 10;
int result;

result = x * 2;

나누기, 나머지 연산

int x = 10;
int result;

result = x / 2;

나머지 연산(%)은 나머지를 구하는 계산!

int x = 10;
int result;

result_1 = x % 2 // 10을 2로 나눈 나머지는 0

result_2 = x % 3 // 10을 3으로 나눈 나머지는 1

구현 팁!

나눗셈 연산 보다 곱하기 연산이 처리가 빠르다!
가능하면 코테나 개발 시 곱하기 우선 처리하자.

x = 10 / 2;
y = 10 * 0.5f;
// y가 더 빠름!

증감 연산

1씩 연산

int x = 10;
x++;
Console.WriteLine(x); // 11

변수에 바로 계산

int x = 10;
Console.WriteLine(x + 10); //20

Console.WriteLine(x); //10
int x = 10;
x = x + 10;
Console.WriteLine(x); //20

+1 을 줄여서 쓰는게 가능하다!

일반 연산은 값을 다시 할당해야 저장된다.
그러나 증감 연산자를 사용하면 다시 할당하지 않아도 값이 저장된다.

코드를 줄일 수 있는 것! -> 메모리 아끼는 것!

int x = 10;
x++;
Console.WriteLine(x); // 11

int y = 10;
y--;
Console.WriteLine(y); // 9;

전위연산/후위연산

int x = 10;
x++; // x를 실행하고 나서 증가
++x; // 먼저 증가하고 나서 x를 실행
int x = 10;
Console.WriteLine(x++); // 아직 10
Console.WriteLine(x); // 코드가 넘어 가서 증가 -> 11

int y = 10;
Console.WriteLine(++y); // 먼저 증가하고 나서 y 출력 -> 11
Console.WriteLine(y); // 11

산술 연산 - 문자열

문자도 계산을 할 수 있다.
바로 + 연산을 활용하는 것.

문자열에서 + 를 하면 문자를 하나로 이어주는 기능을 한다!

string 문자열 변수1 = "값이 많아요";
string 문자열 변수2 = "짱 많아요";

string result = 문자열 변수1 + 문자열 변수2;
Console.WriteLine(result); // 값이 많아요짱 많아요

e.g.,

string hello = "안녕하세요";
string name = "저는 뭉크입니다.";

string result = hello + name;
Console.WriteLine(result); // 안녕하세요저는 뭉크입니다.

문자열과 숫자도 가능!

e.g.,

int month = 7;
string result = month + "월 입니다.";
Console.WriteLine(result);

// 7월 입니다.

논리연산 - 같음 연산자

같은지, 다른지 비교

int num = 10;
bool isSame = num == 10; // true
int num = 10;
bool isSame = num != 10; // false

논리연산 - 비교 연산자

큰지, 작은지 비교

int age = 23;
bool isAdult = age > 19; // true
int age = 23;
bool isKid = age < 19; // false

논리연산 - 조건부 연산자

기호의미
&&AND 연산자
llOR 연산자

eg.,

bool isArrive_A = true;
bool isArrive_B = false;

if(isArrive_A && isArrive_B)
{
    Console.WriteLine("식당으로 이동"); 
}
else
{
    Console.WriteLine("친구를 기다리자"); // A: T, B:F -> A&&B = false
}
bool isArrive_A = true;
bool isArrive_B = true;
if(isArrive_A || isArrive_B)
{
    Console.WriteLine("식당으로 이동"); // A: T, B:T -> A&&B = true
}
else
{
    Console.WriteLine("친구를 기다리자");
}

eg.,

bool isArrive_A = true;
bool isArrive_B = false;

if(isArrive_A || isArrive_B)
{
    Console.WriteLine("식당으로 이동"); // A: T, B:F -> A||B = true
}
else
{
    Console.WriteLine("친구를 기다리자");
}
bool isArrive_A = true;
bool isArrive_B = true;

if(isArrive_A || isArrive_B)
{
    Console.WriteLine("식당으로 이동"); // A: T, B:T -> A||B = true
}
else
{
    Console.WriteLine("친구를 기다리자");
}

자료형 - bool

true, false로 구성.

bool isLarge = 10 > 5; // true

비트 연산

기호e.g.,설명연산자
&a&ba 와 b비트 일치AND
la l ba와 b 비트 중 하나만 일치해도 계산OR
^a^ba와 b 비트가 다른 것 계산XOR
<<a<<b왼쪽 시프트
>>a>>b오른쪽 시프트

만약 내가 게임에 써본다면?

Random rand = new Random();

string nickName = "Munch";
double gachaPer = rand.NextDouble();
double gachaPerLegend = 0.5;

if (gachaPer == gachaPerLegend)
{
    Console.WriteLine(nickName + "님 축하합니다! 레전드 아이템을 획득하였습니다!");
    gachaPer = 0.0; // 확률을 초기화 하려고 넣었는데, 러프하게 생각하고 넣어서 의미 없음.
}
else
{
    Console.WriteLine(nickName + "님, 레전드 아이템 획득에 실패하였습니다.");
}

-> 더블이라 확률이 0.0~1.0인데 소숫점 뒷 자리 범위가 애매하다.

Console.WriteLine(gachaPer); // 0.7737867085986166
Console.WriteLine(gachaPer); // 0.5430570360403487

찍어보니 더블의 모든 자릿 수를 다 사용하는 듯
이대로 냈으면 부자다.

Random rand = new Random();

string nickName = "Munch";
int gachaPer = rand.Next(99, 100);
int gachaPerLegend = 100;

if (gachaPer == gachaPerLegend)
{
    Console.WriteLine(nickName + "님 축하합니다! 레전드 아이템을 획득하였습니다!");
}
else
{
    Console.WriteLine(nickName + "님, 레전드 아이템 획득에 실패하였습니다.");
}

Console.WriteLine(gachaPer);

난 관대하니까 확률을 최고로 올렸다.(이대로 냈어도 부자다.)

운도 없네

확률이 말이 안된다고 생각했는데

Random rand = new Random();
int gachaPer = rand.Next(99, 100);
// 99~99까지다..
Random rand = new Random();

string nickName = "Munch";
int gachaPer = rand.Next(99, 101);
int gachaPerLegend = 100;

if (gachaPer == gachaPerLegend)
{
    Console.WriteLine(nickName + "님 축하합니다! 레전드 아이템을 획득하였습니다!");
}
else
{
    Console.WriteLine(nickName + "님, 레전드 아이템 획득에 실패하였습니다.");
}

Console.WriteLine(gachaPer);


성공!

기억하세요!

int 변수명 = rand.Next(MIN, MAX); // MIN ~ MAX-1까지임!

1개의 댓글

comment-user-thumbnail
2023년 7월 19일

글이 잘 정리되어 있네요. 감사합니다.

답글 달기