int score = 100;
string playerRank = "";
if (score >= 90)
{
playerRank = "Diamond";
}
else
{
if (score >= 80)
{
playerRank = "platinum";
}
else
{
if (score >= 70)
{
playerRank = "Gold";
}
else
{
if (score >= 60)
{
playerRank = "Silver";
}
else
{
playerRank = "Bronze";
}
}
}
}
Console.WriteLine("플레이어의 등급은 {0}입니다.", playerRank);
int score = 100;
string playerRank = "";
if (score >= 90)
{
playerRank = "Diamond";
}
else if (score >= 80)
{
playerRank = "platinum";
}
else if (score >= 70)
{
playerRank = "Gold";
}
else if (score >= 60)
{
playerRank = "Silver";
}
else
{
playerRank = "Bronze";
}
Console.WriteLine("플레이어의 등급은 {0}입니다.", playerRank);
}
int itemLevel = 3; // 아이템 레벨
string itemType = "Weapon"; // 아이템 종류
if (itemType == "Weapon")
{
if (itemLevel == 1)
{
// 레벨 1 무기 효과
Console.WriteLine("공격력이 10 증가했습니다.");
}
else if (itemLevel == 2)
{
// 레벨 2 무기 효과
Console.WriteLine("공격력이 20 증가했습니다.");
}
else
{
// 그 외 무기 레벨
Console.WriteLine("잘못된 아이템 레벨입니다.");
}
}
else if (itemType == "Armor")
{
if (itemLevel == 1)
{
// 레벨 1 방어구 효과
Console.WriteLine("방어력이 10 증가했습니다.");
}
else if (itemLevel == 2)
{
// 레벨 2 방어구 효과
Console.WriteLine("방어력이 20 증가했습니다.");
}
else
{
// 그 외 방어구 레벨
Console.WriteLine("잘못된 아이템 레벨입니다.");
}
}
else
{
// 그 외 아이템 종류
Console.WriteLine("잘못된 아이템 종류입니다.");
}
switch (job)
{
case "1":
Console.WriteLine("전사를 선택하셨습니다.");
break;
case "2":
Console.WriteLine("마법사를 선택하셨습니다.");
break;
case "3":
Console.WriteLine("궁수를 선택하셨습니다.");
break;
default: // 생략 가능
Console.WriteLine("올바른 값을 입력해 주세요.");
break;
}
int currentExp = 1200;
int requiredExp = 2000;
# 삼항 연산자
string result = (currentExp >= requiredExp) ? "레벨업 가능" : "레벨업 불가능";
Console.WriteLine(result);
# if else 문
if (currentExp >= requiredExp)
{
Console.WriteLine("레벨업 가능");
}
else
{
Console.WriteLine("레벨업 불가능");
}
// 1. 홀수와 짝수 구분하기
Console.Write("번호를 입력하세요: ");
int number = int.Parse(Console.ReadLine());
if (number % 2 == 0)
{
Console.WriteLine("짝수입니다.");
}
else
{
Console.WriteLine("홀수입니다.");
}
// 2. 등급 출력하기
int playerScore = 100;
string playerRank = "";
switch(playerScore / 10)
{
case 10:
case 9:
playerRank = "Diamond";
break;
case 8:
playerRank = "Platinum";
break;
case 7:
playerRank = "Gold";
break;
case 6 :
playerRank = "Silver";
break;
default:
playerRank = "Bronze";
break;
}
Console.WriteLine(playerRank);
// 3. 로그인 프로그램
string id = "id";
string password = "pw";
Console.Write("아이디를 입력하세요: ");
string inputId = Console.ReadLine();
Console.Write("비밀번호를 입력하세요: ");
string inputPassword = Console.ReadLine();
if (id == inputId && password == inputPassword)
{
Console.WriteLine("로그인 성공!");
}
else
{
Console.WriteLine("로그인 실패...");
}
// 4. 알파벳 판별 프로그램
Console.Write("문자를 입력하세요: ");
char input = Console.ReadLine()[0];
// 소문자에 포함되면서 대문자에 포함되면
if ( (input >= 'a' && input <= 'z') || (input >= 'A' && input <= 'Z') )
{
Console.WriteLine("알파벳입니다.");
}
else
{
Console.WriteLine("알파벳이 아닙니다.");
}
for (초기식; 조건식; 증감식)
{
// 조건식이 참인 경우 실행되는 코드
}
// for문 기초
for (int i = 0; i < 10; i++)
{
Console.WriteLine(i);
}
for (int i = 1; i < 21; i += 2)
{
Console.WriteLine(i);
}
int i = 0;
for (i = 1; i < 21; i += 2)
{
Console.WriteLine(i);
}
while (조건식)
{
// 조건식이 참인 경우 실행되는 코드
}
// while문 기초
int i = 0;
while (i < 10)
{
Console.WriteLine(i);
i++;
}
// while문 예시
int count = 0;
while (count < 10)
{
Console.WriteLine("적을 처치했습니다! 남은 적 수: " + (10 - count - 1));
count++;
}
Console.WriteLine("축하합니다! 게임에서 승리하셨습니다!");
int sum = 0;
for (int i = 1; i <= 5; i++)
{
sum += i;
}
Console.WriteLine("1부터 5까지의 합은 " + sum + "입니다.");
int sum = 0;
int i = 1;
while (i <= 5)
{
sum += i;
i++;
}
Console.WriteLine("1부터 5까지의 합은 " + sum + "입니다.");
do
{
// 조건식이 참인 경우 실행되는 코드
}
while (조건식);
int sum = 0;
int num;
do
{
Console.Write("숫자를 입력하세요 (0 입력 시 종료): ");
num = int.Parse(Console.ReadLine());
sum += num;
} while (num != 0);
Console.WriteLine("합계: " + sum);
foreach (자료형 변수 in 배열 또는 컬렉션)
{
// 배열 또는 컬렉션의 모든 요소에 대해 반복적으로 실행되는 코드
}
string[] inventory = { "검", "방패", "활", "화살", "물약" };
foreach (string item in inventory)
{
Console.WriteLine(item);
}
반복문을 한번 더 돌리는 것
이차원 반복문
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 3; j++)
{
Console.WriteLine("i: {0}, j: {1}", i, j);
}
}
// 구구단 출력
for (int i = 2; i <= 9; i++)
{
for (int j = 1; j <= 9; j++)
{
Console.WriteLine(i + " x " + j + " = " + (i * j));
}
}
for (int i = 2; i <= 9; i++)
{
for (int j = 1; j <= 9; j++)
{
Console.Write(i + " x " + j + " = " + (i * j) + "\t");
}
Console.WriteLine();
}
for (int i = 1; i <= 9; i++)
{
for (int j = 2; j <= 9; j++)
{
Console.Write(j + " x " + i + " = " + (i * j) + "\t");
}
Console.WriteLine();
}
// break와 continue 기초
for (int i = 1; i <= 10; i++)
{
Console.WriteLine(i);
}
for (int i = 1; i <= 10; i++)
{
if (i % 3 == 0)
{
continue;
}
Console.WriteLine(i);
}
for (int i = 1; i <= 10; i++)
{
if (i % 3 == 0)
{
continue;
}
Console.WriteLine(i);
if (i == 7)
{
break;
}
}
// break와 continue 예제
int sum = 0;
while (true) // 무한루프
{
Console.Write("숫자를 입력하세요: ");
int input = int.Parse(Console.ReadLine());
}
Console.WriteLine("합계: " + sum);
int sum = 0;
while (true) // 무한 루프, for(;;)
{
Console.Write("숫자를 입력하세요: ");
int input = int.Parse(Console.ReadLine());
if (input == 0)
{
Console.WriteLine("프로그램 종료");
break;
}
}
Console.WriteLine("합계: " + sum);
int sum = 0;
while (true) // 무한 루프, for(;;)
{
Console.Write("숫자를 입력하세요: ");
int input = int.Parse(Console.ReadLine());
if (input == 0)
{
Console.WriteLine("프로그램 종료");
break;
}
if (input < 0)
{
Console.WriteLine("음수는 무시");
continue;
}
}
Console.WriteLine("합계: " + sum);
int sum = 0;
while (true) // 무한 루프, for(;;)
{
Console.Write("숫자를 입력하세요: ");
int input = int.Parse(Console.ReadLine());
if (input == 0)
{
Console.WriteLine("프로그램 종료");
break;
}
if (input < 0)
{
Console.WriteLine("음수는 무시");
continue;
}
sum += input;
Console.WriteLine(sum);
}
Console.WriteLine("합계: " + sum);
// 1. 가위 바위 보
string[] choice = { "가위", "바위", "보" };
string[] choice = { "가위", "바위", "보" };
string playerCoice = "";
string computerCoice = choice[new Random().Next(0, 3)];
string[] choice = { "가위", "바위", "보" };
string playerCoice = "";
string computerCoice = choice[new Random().Next(0, 3)];
while (playerCoice != computerCoice)
{
Console.Write("가위, 바위, 보 중 하나를 선택하세요: ");
playerCoice = Console.ReadLine();
}
string[] choice = { "가위", "바위", "보" };
string playerCoice = "";
string computerCoice = choice[new Random().Next(0, 3)];
while (playerCoice != computerCoice)
{
Console.Write("가위, 바위, 보 중 하나를 선택하세요: ");
playerCoice = Console.ReadLine();
if (playerCoice == computerCoice)
{
Console.WriteLine("비겼습니다.");
}
else if
{
}
}
while (playerCoice != computerCoice)
{
Console.Write("가위, 바위, 보 중 하나를 선택하세요: ");
playerCoice = Console.ReadLine();
if (playerCoice == computerCoice)
{
Console.WriteLine("비겼습니다.");
}
else if (
(playerCoice == "가위" && computerCoice == "보") ||
(playerCoice == "바위" && computerCoice == "가위") ||
(playerCoice == "보" && computerCoice == "바위")
)
{
Console.WriteLine("플레이어 승리!");
}
else
{
Console.WriteLine("컴퓨터 승리!");
}
}
// 2, 숫자 맞추기
int targetNumber = new Random().Next(1, 101);
int guess = 0;
int count = 0;
Console.WriteLine("1부터 100 사이의 숫자를 맞춰보세요.");
while (guess != targetNumber)
{
}
int targetNumber = new Random().Next(1, 101);
int guess = 0;
int count = 0;
Console.WriteLine("1부터 100 사이의 숫자를 맞춰보세요.");
while (guess != targetNumber)
{
Console.Write("추측한 숫자를 입력하세요. ");
guess = int.Parse(Console.ReadLine());
count++;
int targetNumber = new Random().Next(1, 101);
int guess = 0;
int count = 0;
Console.WriteLine("1부터 100 사이의 숫자를 맞춰보세요.");
while (guess != targetNumber)
{
Console.Write("추측한 숫자를 입력하세요. ");
guess = int.Parse(Console.ReadLine());
count++;
if (guess < targetNumber)
{
Console.WriteLine("좀 더 큰 숫자를 입력하세요.");
}
else if (guess > targetNumber)
{
Console.WriteLine("좀 더 작은 숫자를 입력하세요.");
}
else
{
Console.WriteLine("축하합니다! 숫자를 맞추셨습니다.");
Console.WriteLine("시도한 횟수 : " + count);
}
// 배열 선언
데이터_유형[] 배열_이름;
// 배열 초기화
배열_이름 = new 데이터_유형[크기];
// 배열을 한 줄로 선언 및 초기화
데이터_유형[] 배열_이름 = new 데이터_유형[크기];
// 배열 요소에 접근
배열_이름[인덱스] = 값;
값 = 배열_이름[인덱스];
int[] array1 = new int[5]; // 크기가 5인 int형 배열 선언
string[] array2 = new string[3]; // 크기가 3인 string형 배열 선언
int num = 0;
// 배열 초기화
array1[0] = 1;
array1[1] = 2;
array1[2] = 3;
array1[3] = 4;
array1[4] = 5;
num = array1[0]; // 사용 시 이렇게 적어주면 0번째에 있는 값이 반환됨
// 예제
int[] itemPrices = { 100, 200, 300, 400, 500 };
int totalPrice = 0;
for (int i = 0; i < itemPrices.Length; i++)
{
totalPrice += itemPrices[i];
}
Console.WriteLine("총 아이템 가격: " + totalPrice + " gold");
// 플레이어의 공격력, 방어력, 체력, 스피드를 저장할 배열
int[] playerStats = new int[4];
// 능력치를 랜덤으로 생성하여 배열에 저장
Random rand = new Random();
for (int i = 0; i < playerStats.Length; i++)
{
playerStats[i] = rand.Next(1, 11);
}
// 능력치 출력
Console.WriteLine("플레이어의 공격력: " + playerStats[0]);
Console.WriteLine("플레이어의 방어력: " + playerStats[1]);
Console.WriteLine("플레이어의 체력: " + playerStats[2]);
Console.WriteLine("플레이어의 스피드: " + playerStats[3]);
// 학생들의 성적 평균 구하기
int[] scores = new int[5];
for (int i = 0; i < scores.Length; i++)
{
Console.Write("학생 " + (i + 1) + "의 성적을 입력하세요: ");
scores[i] = int.Parse(Console.ReadLine());
}
int sum = 0;
for (int i = 0; i < scores.Length; i++)
{
sum += scores[i];
}
double average = sum / scores.Length;
Console.WriteLine("성적 평균은 " + average + "입니다.");
int[] scores = new int[5];
for (int i = 0; i < scores.Length; i++)
{
Console.Write("학생 " + (i + 1) + "의 성적을 입력하세요: ");
scores[i] = int.Parse(Console.ReadLine());
}
int sum = 0;
for (int i = 0; i < scores.Length; i++)
{
sum += scores[i];
}
double average = (double)sum / scores.Length;
Console.WriteLine("성적 평균은 " + average + "입니다.");
// 배열을 활용한 숫자 맞추기 게임
Random random = new Random(); // 랜덤 객체 생성
int[] numbers = new int[3]; // 3개의 숫자를 저장할 배열
// 3개의 랜덤 숫자 생성하여 배열에 저장
for (int i = 0; i < numbers.Length; i++)
{
numbers[i] = random.Next(1, 10);
}
int attempt = 0; // 시도 횟수 초기화
while (true)
{
Console.Write("3개의 숫자를 입력하세요 (1~9): ");
int[] guesses = new int[3]; // 사용자가 입력한 숫자를 저장할 배열
}
Random random = new Random(); // 랜덤 객체 생성
int[] numbers = new int[3]; // 3개의 숫자를 저장할 배열
// 3개의 랜덤 숫자 생성하여 배열에 저장
for (int i = 0; i < numbers.Length; i++)
{
numbers[i] = random.Next(1, 10);
}
int attempt = 0; // 시도 횟수 초기화
while (true)
{
Console.Write("3개의 숫자를 입력하세요 (1~9): ");
int[] guesses = new int[3]; // 사용자가 입력한 숫자를 저장할 배열
for (int i = 0; i < numbers.Length; i++)
{
guesses[i] = int.Parse(Console.ReadLine());
}
}
for (int i = 0; i < numbers.Length; i++)
{
guesses[i] = int.Parse(Console.ReadLine());
}
int correct = 0;
for (int i = 0; i < numbers.Length; i++)
{
for (int j = 0; j < guesses.Length; j++)
{
}
}
int correct = 0;
for (int i = 0; i < numbers.Length; i++)
{
for (int j = 0; j < guesses.Length; j++)
{
if (numbers[i] == guesses[j])
{
correct++;
break;
}
}
}
int correct = 0;
for (int i = 0; i < numbers.Length; i++)
{
for (int j = 0; j < guesses.Length; j++)
{
if (numbers[i] == guesses[j])
{
correct++;
break;
}
}
}
attempt++;
Console.WriteLine("시도 #" + attempt + " : " + correct + "개의 숫자를 맞추셨습니다.");
}
attempt++;
Console.WriteLine("시도 #" + attempt + " : " + correct + "개의 숫자를 맞추셨습니다.");
if (correct == 3)
{
Console.WriteLine("축하합니다! 모든 숫자를 맞추셨습니다.");
break;
}
// 2차원 배열의 선언과 초기화
int[,] array3 = new int[2, 3]; // 2행 3열의 int형 2차원 배열 선언
// 다차원 배열 초기화
array3[0, 0] = 1;
array3[0, 1] = 2;
array3[0, 2] = 3;
array3[1, 0] = 4;
array3[1, 1] = 5;
array3[1, 2] = 6;
// 선언과 함께 초기화
int[,] array2D = new int[3, 4] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
// 3차원 배열의 선언과 초기화
int[,,] array3D = new int[2, 3, 4]
{
{ { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } },
{ { 13, 14, 15, 16 }, { 17, 18, 19, 20 }, { 21, 22, 23, 24 } }
};
int[,] map = new int[5, 5];
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
map[i, j] = i + j;
}
}
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
Console.Write(map[i, j] + " ");
}
Console.WriteLine();
}
// 2차원 배열을 사용하여 게임 맵을 구현
int[,] map = new int[5, 5]
{
{ 1, 1, 1, 1, 1 },
{ 1, 0, 0, 0, 1 },
{ 1, 0, 1, 0, 1 },
{ 1, 0, 0, 0, 1 },
{ 1, 1, 1, 1, 1 },
};
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
if (map[i, j] == 1)
{
Console.Write("■ ");
}
else
{
Console.Write("□ ");
}
}
Console.WriteLine();
}
List<int> numbers = new List<int>(); // 빈 리스트 생성
numbers.Add(1); // 리스트에 데이터 추가
numbers.Add(2);
numbers.Add(3);
numbers.Remove(2); // 리스트에서 데이터 삭제
foreach(int number in numbers) // 리스트 데이터 출력
{
Console.WriteLine(number);
}
List<int> list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(3);
list.Remove(2);
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine(list[i]);
}
using System.Collections.Generic;
Dictionary<string, int> scores = new Dictionary<string, int>(); // 빈 딕셔너리 생성
scores.Add("Alice", 100); // 딕셔너리에 데이터 추가
scores.Add("Bob", 80);
scores.Add("Charlie", 90);
scores.Remove("Bob"); // 딕셔너리에서 데이터 삭제
foreach(KeyValuePair<string, int> pair in scores) // 딕셔너리 데이터 출력
{
Console.WriteLine(pair.Key + ": " + pair.Value);
}
Stack<int> stack1 = new Stack<int>(); // int형 Stack 선언
// Stack에 요소 추가
stack1.Push(1);
stack1.Push(2);
stack1.Push(3);
// Stack에서 요소 가져오기
int value = stack1.Pop(); // value = 3 (마지막에 추가된 요소)
Queue<int> queue1 = new Queue<int>(); // int형 Queue 선언
// Queue에 요소 추가
queue1.Enqueue(1);
queue1.Enqueue(2);
queue1.Enqueue(3);
// Queue에서 요소 가져오기
int value = queue1.Dequeue(); // value = 1 (가장 먼저 추가된 요소)
HashSet<int> set1 = new HashSet<int>(); // int형 HashSet 선언
// HashSet에 요소 추가
set1.Add(1);
set1.Add(2);
set1.Add(3);
// HashSet에서 요소 가져오기
foreach (int element in set1)
{
Console.WriteLine(element);
}