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가 더 빠름!
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
일반 연산은 값을 다시 할당해야 저장된다.
그러나 증감 연산자를 사용하면 다시 할당하지 않아도 값이 저장된다.
코드를 줄일 수 있는 것! -> 메모리 아끼는 것!
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 연산자 |
ll | OR 연산자 |
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("친구를 기다리자");
}
true, false로 구성.
bool isLarge = 10 > 5; // true
기호 | e.g., | 설명 | 연산자 |
---|---|---|---|
& | a&b | a 와 b비트 일치 | AND |
l | a l b | a와 b 비트 중 하나만 일치해도 계산 | OR |
^ | a^b | a와 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까지임!
글이 잘 정리되어 있네요. 감사합니다.