일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
- dynamic
- 시작해요 언리얼 2022
- Unity
- String
- parameter
- 오류
- Algorithm
- w3school
- 문제풀이
- github
- dfs
- Basic
- c++
- 백준
- guide
- python
- UE5
- Programming
- Class
- 파이썬
- Unreal Engine 5
- 재귀
- Material
- loop
- DP
- C#
- Tutorial
- 기초
- 프로그래밍
- W3Schools
- Today
- Total
행복한 개구리
C# 21.03.10.수업내용 본문
switch 복습
namespace Homework000
{
class Program
{
static void Main(string[] args)
{
int selectedCharacterId = 100; //유저가 캐릭터를 선택
switch (selectedCharacterId)
{
case 100: //선택에 따른 행동
{
string name = "야만전사";
Console.WriteLine("야만전사를 선택했습니다.");
Console.WriteLine("야만전사가 선택 애니메이션을 취합니다.");
}
break;
case 200:
{//블록을 사용하면 각 case에서 이름이 같은 변수 사용 가능
string name = "악마사냥꾼";
}
break;
case 300:
break;
case 400:
break;
case 500:
break;
}
}
}
}
아래처럼 name을 선정의 한 뒤 값을 case안에서 할당할 수 있음.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework000
{
class Program
{
static void Main(string[] args)
{
int selectedCharacterId = 100;
string name;
switch (selectedCharacterId)
{
case 100:
{
name = "야만전사";
Console.WriteLine("야만전사를 선택했습니다.");
Console.WriteLine("야만전사가 선택 애니메이션을 취합니다.");
}
break;
case 200:
{
name = "악마 사냥꾼";
}
break;
case 300:
break;
case 400:
break;
case 500:
break;
}
}
}
}
============================================================================
Console.ReadLine()
// 메서드이다. 메서드의 기능을 완료(사용이 끝)하면 string타입으로 값을 반환(호출, 수행)해준다.(반환타입 string)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework000
{
class Program
{
static void Main(string[] args)
{
int i = 0;
while (i < 1)
{
Console.WriteLine("과일 이름을 입력하세요");
string input = Console.ReadLine();
Console.WriteLine("{0}을 입력하셨습니다.", input);
if (input == "바나나")
{
Console.WriteLine("바나나는 파초과 파초속의 여러해살이 식물과 열매를 두루 일컫는 말이다. " +
"바나나는 열대 아시아, 인도, 말레이시아 등지가 바나나의 원산지이지만, 현재의 주된 바나나 생산 지역은 인도, 브라질, 필리핀, 에콰도르 등이다.");
}
else if (input == "수박")
{
Console.WriteLine("수박은 남아프리카 원산의 한해살이 덩굴식물, 또는 그 열매를 말한다. " +
"서과 또는 수과라고도 한다. 열매의 속살은 식용하는데, 붉거나 노란색을 띠며, 달고, 씨가 있는 수박이 대부분이다.");
}
else
{
Console.WriteLine("{0}에 대한 정보가 부족합니다.", input);
}
}
}
}
}
if - else if - else 문과 while문으로 무한루프로 입력을 계속 할 수 있게 설정
============================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework000
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i<5; i++)
{
Console.WriteLine("과일 이름을 입력하세요");
string input = Console.ReadLine();
Console.WriteLine("{0}을 입력하셨습니다.", input);
switch (input)
{
case "바나나":
Console.WriteLine("바나나는 파초과 파초속의 여러해살이 식물과 열매를 두루 일컫는 말이다. " +
"바나나는 열대 아시아, 인도, 말레이시아 등지가 바나나의 원산지이지만, 현재의 주된 바나나 생산 지역은 인도, 브라질, 필리핀, 에콰도르 등이다.");
break;
case "수박":
Console.WriteLine("수박은 남아프리카 원산의 한해살이 덩굴식물, 또는 그 열매를 말한다. 서과 또는 수과라고도 한다. " +
"열매의 속살은 식용하는데, 붉거나 노란색을 띠며, 달고, 씨가 있는 수박이 대부분이다.");
break;
case "종료":
Console.WriteLine("프로그램을 종료합니다.");
break;
default:
Console.WriteLine("정보가 부족합니다.");
break;
}
}
}
}
}
위의 if문과 while문으로 반복한것을 for문과 switch문으로 변환해 봄.
============================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework000
{
class Program
{
static void Main(string[] args)
{
string input = Console.ReadLine();
if (input == "100")
{
}
//string (숫자형 문자열)
//(ex) '1', '100'
//int num = (int)input; - 실행안됨
int num = Convert.ToInt32(input); // 성공
if(num == 100)
{
}
//int -> string
string str = num.ToString();
Console.WriteLine(num.ToString());
if (str == "100")
{
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework000
{
class Program
{
static void Main(string[] args)
{
string input = Console.ReadLine();
int num = Convert.ToInt32(input);
if (num>=1 && num <= 10)
{
for(int i = 0; i < num; i++)
{
Console.WriteLine("줄넘기를 {0}회 했습니다.", i+1);
}
}
else
{
Console.WriteLine("범위를 벗어났습니다.");
}
//(숫자)문자열 값을 입력받아 출력 하세요 (범위 : 1~10)
//정수로 변환 하세요.
//입력된 값이 범위를 벗어난다면 "범위를 벗어났습니다."출력
//변환된 값만큼 반복문을 실행합니다.(for)
//출력 결과는
//줄넘기를 1회 했습니다.
//줄넘기를 2회 했습니다.
//줄넘기를 3회 했습니다.
//....
//줄넘기를 n회 했습니다.
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework000
{
class Program
{
static void Main(string[] args)
{
string input = Console.ReadLine();
int select = Convert.ToInt32(input);
string name = "";
switch (select)
{
case 1:
name = "야만전사";
Console.WriteLine("{0}을(를) 선택 했습니다.", name);
break;
case 2:
name = "악마 사냥꾼";
Console.WriteLine("{0}을(를) 선택 했습니다.", name);
break;
case 3:
name = "부두술사";
Console.WriteLine("{0}을(를) 선택 했습니다.", name);
break;
default:
Console.WriteLine("다시 입력하세요.");
break;
}
//입력 받은 문자열에 따라 캐릭터를 선택 하세요
//입력 예시 : 야만전사, 악마 사냥꾼, 부두술사
//switch문을 사용하세요
//출력 결과
//case에 따라...
//야만전사를 선택 했습니다.
//악마 사냥꾼을 선택 했습니다.
//부두술사를 선택 했습니다.
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study00
{
class Program
{
static void Main(string[] args)
{
string input = Console.ReadLine();
int num = Convert.ToInt32(input);
int i = 0;
while (i < 1)
{
if (num > 3)
{
Console.WriteLine("2 * {0} = {1}", num, 2 * num);
num++;
}
else
{
Console.WriteLine("숫자를 잘못 입력하였습니다.");
break;
}
}
//입력받은 문자열값을 정수로 변환 하세요
//while문을 통해 무한 반복하세요
//정수로 변환된 값이 만약에 3보다 같거나 작다면 break문으로 반복문을 종료 하세요
//while문 실행시 다음과 같이 출력 합니다.
//2 * 1 = 2
//2 * 2 = 4
//2 * 3 = 6
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study00
{
class Program
{
enum tribe
{
Terran,
Protoss,
Zerg
}
static void Main(string[] args)
{
string input = Console.ReadLine();
int num = Convert.ToInt32(input);
tribe selected = (tribe)num;
if (num <= 2 && num >= 0)
{
Console.WriteLine("{0}을 선택했습니다.", selected);
}
else
{
Console.WriteLine("다시 선택하십시오.");
}
//Enum을 정의 합니다.
//Terran, Protoss, Zerg
//입력을 받습니다.
//입력값을 숫자로 변환 합니다.
//변환된 정수값을 Enum형식으로 변환 합니다.
//선택문을 통해 선택한 종족을 출력 합니다.
//출력결과
//Terran을 선택했습니다.
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study00
{
class Program
{
static void Main(string[] args)
{
int i = 0;
while (i < 1)
{
string input = Console.ReadLine();
if (input == "종료")
{
Console.WriteLine("종료합니다.");
break;
}
int num = Convert.ToInt32(input);
string input2 = Console.ReadLine();
int num2 = Convert.ToInt32(input2);
if (num < -100 ^ num > 100 || num2 < -100 ^ num2 > 100)
{
Console.WriteLine("다시 입력하십시오.");
continue;
}
else if (num <= 100 && num >= -100)
{
Console.WriteLine("a : {0}", input);
Console.WriteLine("b : {0}", input2);
Console.WriteLine("sum : {0}", num + num2);
continue;
}
}
//입력받습니다.
//정수로 변환
//입력 받습니다.
//정수로 변환
//두 정수의 합을 출력 하세요
//정수의 범위 (-100 ~ 100)
//출력 결과
//a : 10
//b : -2
//sum : 8
}
}
}
입력값(숫자)의 합 구하기
============================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study00
{
class Program
{
static void Main(string[] args)
{
int result = Sum(1, 2);
Console.WriteLine(result);
/*class는 (정보+기능) 이다.
* 메서드도 변수와 마찬가지로 선언을 먼저 해야한다.
*메서드는 동사로 시작하며 대문자로 시작한다.
*메서드를 호출(실행)하려면 메서드 이름뒤에 두 개의 괄호()와 세미콜론을 입력한다.
*접근제한자는 private과 public이 있다.(etc)
*1. 어떤 기능을 입력할지 생각하고
*2. 메서드를 정의하고
*3. 그것을 구현한다,
*4. 그리고 그것을 호출한다.
*5. 메서드는 호출이 끝나면 제 자리로 돌아온다.
*ㄴ 메서드의 호출이 끝나면 그 시점에서 입력됐던 메모리들은 모두 사라진다.
* 메서드 선언은 class 내부 어디서든 가능하다.
* 메서드의 ()안에 선언하는 변수가 '매개변수'이다.
* 메서드의 타입은 메서드의 반환타입과 같아야한다 ex) string -> string
*/
}
//접근제한자 : private, public
//접근제한자 반환타입 이름(매개변수)
//{
// 메서드 본문
//}
//메서드 정의
static private void SayHello()
{
Console.WriteLine("Hello~");
}
static private int Sum(int a, int b)
{
int sum = a + b;
return sum;
}
}
}
메서드(반환타입o/매개변수o)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study00
{
class Program
{
static void Main(string[] args)
{
MoveEast();
MoveEast();
MoveNorth();
MoveNorth();
MoveWest();
MoveSouth();
EquipItem();
}
//1.동쪽으로 이동
static private void MoveEast() //2 이름설정
{
Console.WriteLine("동쪽으로 이동했습니다.");
}
static private void MoveWest()
{
Console.WriteLine("서쪽으로 이동했습니다.");
}
static private void MoveSouth()
{
Console.WriteLine("남쪽으로 이동했습니다.");
}
static private void MoveNorth()
{
Console.WriteLine("북쪽으로 이동했습니다.");
}
//아이템착용
static private void EquipItem()
{
Console.WriteLine("아이템을 착용했습니다.");
}
}
}
메서드 (반환타입x/매개변수x)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study00
{
class Program
{
static void Main(string[] args)
{
Move("동");
Move("서");
Move("남");
Move("북");
Move("내맘대로");
}
static private void Move(string a) //★ 메서드를 string 타입으로 지정하지 않음.
{
if (a == "동")
{
Console.WriteLine("{0}쪽으로 갑니다.", a);
}
else if (a == "서")
{
Console.WriteLine("{0}쪽으로 갑니다.", a);
}
else if (a == "남")
{
Console.WriteLine("{0}쪽으로 갑니다.", a);
}
else if (a == "북")
{
Console.WriteLine("{0}쪽으로 갑니다.", a);
}
else
{
Console.WriteLine("잘못된 값입니다.");
}
}
}
}
메서드(반환타입x/매개변수o)
반환타입 주의 (switch로 만들어보기.)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study00
{
class Program
{
static void Main(string[] args)
{
Move("동쪽");
Attack("홍길동");
UseSkill("홍길동 혼내주기");
Reload(6);
Taunt("홍길동");
GainItem("홍길동 브레이커");
UseItem("공격력 증가 포션");
Equip("판금 갑옷");
Unequip("누더기 갑옷");
Enhance("홍길동 브레이커");
ChargeMoney(24000);
Recall("고블린 막사");
Compose("홍길동 브레이커", "고블린 수호자 망치");
Enter("활빈당 주둔지");
}
//이동한다 (방향)
static private void Move(string a)
{
switch (a)
{
case "북쪽":
Console.WriteLine("{0}으로 이동한다.", a);
break;
case "남쪽":
Console.WriteLine("{0)으로 이동한다.", a);
break;
case "동쪽":
Console.WriteLine("{0}으로 이동한다.", a);
break;
case "서쪽":
Console.WriteLine("{0}으로 이동한다.", a);
break;
}
}
//공격한다 (타겟이름)
static private void Attack (string enemy)
{
Console.WriteLine("{0}을 공격합니다.", enemy);
}
//스킬을 사용한다 (스킬이름)
static private void UseSkill (string skillName)
{
Console.WriteLine("{0}을 사용합니다.", skillName);
}
//총알을 장전한다 (몇발)
static private void Reload(int amount)
{
Console.WriteLine("총알을 {0}발 장전했습니다.", amount);
}
//도발한다 (타겟이름)
static private void Taunt(string enemy)
{
Console.WriteLine("{0}을 도발했습니다.", enemy);
}
//아이템을 줍는다 (아이템이름)
static private void GainItem(string itemName)
{
Console.WriteLine("{0}을 획득했습니다.", itemName);
}
//아이템을 사용한다 (아이템이름)
static private void UseItem(string itemName)
{
Console.WriteLine("{0}을 사용합니다.", itemName);
}
//장비를 착용한다 (장비 이름)
static private void Equip(string equipmentName)
{
Console.WriteLine("{0}을(를) 착용합니다.", equipmentName);
}
//장비를 벗는다 (장비이름)
static private void Unequip(string equipmentName)
{
Console.WriteLine("{0}을(를) 착용해제합니다.", equipmentName);
}
//아이템을 강화한다. (아이템 이름)
static private void Enhance(string itemName)
{
Console.WriteLine("{0}을(를) 강화합니다.", itemName);
}
//요금을 충전한다 (얼마)
static private void ChargeMoney(int num)
{
Console.WriteLine("{0}원을 충전합니다." , num);
}
//합성한다 (아이템1, 아이템2)
static private void Compose(string item1, string item2)
{
Console.WriteLine("{0}과(와) {1}을(를) 합성합니다.", item1, item2);
}
//귀환한다. (어디로)
static private void Recall(string where)
{
Console.WriteLine("{0}로 돌아갑니다.", where);
}
//입장한다 (어디로)
static private void Enter(string where)
{
Console.WriteLine("{0}에 입장합니다.", where);
}
}
}
메서드(반환타입x/매개변수o)
고블린 대폭 상향안(Compose -> Synthesize)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study00
{
class Program
{
static void Main(string[] args)
{
}
//매개변수가 없고 반환값이 있는 메서드 정의
//드론생성
private string CreateDron()
{
string unitName = "드론";
return unitName;
}
private string CreateDron()
{
return "드론";
}
private int MineMineral()
{
int amount = 8;
return amount;
}
private int SynthesizeItem()
{
int amount = 8;
return amount;
}
//낚시하다
private string Fishing()
{
string fishName = "참치";
return fishName;
}
private string PickCard()
{
return "스페이드 A";
}
private string Build()
{
string buildingName = "배럭스";
return buildingName;
}
private string BrewCoffee()
{
string coffeeName = "아메리카노";
return coffeeName;
}
}
}
메서드(반환타입o/매개변수x)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study00
{
class Program
{
static void Main(string[] args)
{
string unitName = CreateDron();
Console.WriteLine("{0}을 생성하였습니다.", unitName);
int amount = MineMineral();
Console.WriteLine("미네랄을 {0} 채굴했습니다.", amount);
string fishName = Fishing();
Console.WriteLine("{0}을 낚았습니다.", fishName);
string cardName = PickCard();
Console.WriteLine("{0}을 뽑았습니다.", cardName);
string buildingName = Build();
Console.WriteLine("{0}을 건설했습니다.", buildingName);
string coffeeName = BrewCoffee();
Console.WriteLine("{0}을 내렸습니다.", coffeeName);
}
//매개변수가 없고 반환값이 있는 메서드 정의
//드론생성
static private string CreateDron()
{
string unitName = "드론";
return unitName;
}
static private int MineMineral()
{
int amount = 8;
return amount;
}
//낚시하다
static private string Fishing()
{
string fishName = "참치";
return fishName;
}
static private string PickCard()
{
return "스페이드 A";
}
static private string Build()
{
string buildingName = "배럭스";
return buildingName;
}
static private string BrewCoffee()
{
string coffeeName = "아메리카노";
return coffeeName;
}
}
}
메서드(반환타입o/매개변수x)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study00
{
class Program
{
public enum eRace
{
Terran,
Protoss,
Zerg
}
static void Main(string[] args)
{
int gold = 151;
string buildingName = Build(gold);
if (buildingName == "")
{
Console.WriteLine("건설에 실패하였습니다.");
}
else
{
Console.WriteLine("{0} 건설을 시작합니다.", buildingName);
}
}
private string Synthesize(string itemName1, string itemName2)
{
Console.WriteLine("{0}과 {1}를 합성합니다.", itemName1, itemName2);
string itemName = "강화 도끼";
return itemName;
}
//사칙연산
private float Calculate(float a, float b, string strOperator)
{
float result = 0;
if (strOperator == "+")
{
result = a + b;
}
return result;
}
//회원가입
private bool SignUp(string email, string password)
{
return true;
}
//스킬연계
private string LinkSkill(string skill1, string skill2)
{
return "새로운 스킬";
}
//캐릭터 생성
private string CreateCharacter(string name, string weaponName)
{
return name + "가" + weaponName + "을 착용했습니다.";
}
private string EquipItem(string name, string itemName)
{
return name + "에게" + itemName + "이 귀속되었습니다.";
}
private eRace IntToEnum(int num)
{
return (eRace)num;
}
//래핑 메서드 예시
//변환
private string ConvertToUpperCase(string str)
{
return str.ToUpper() + "123";
}
static private string Build(int gold)
{
if (gold > 150)
{
return "배럭스";
}
else
{
return "";
}
}
}
}
메서드 (반환타입o/매개변수o)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study00
{
class Program
{
public enum eRace
{
Terran,
Protoss,
Zerg
}
static void Main(string[] args)
{
//건설
int gold = 151;
string buildingName = Build(gold);
if (buildingName == "")
{
Console.WriteLine("건설에 실패하였습니다.");
}
else
{
Console.WriteLine("{0} 건설을 시작합니다.", buildingName);
}
//합성
string itemName = Synthesize("강화의 불꽃", "단도");
Random rand = new Random();
int num = rand.Next(1,101);
if (num > 20)
{
Console.WriteLine("합성에 실패하였습니다.");
}
else
{
Console.WriteLine("합성에 성공하여 {0}를 획득했습니다.", itemName);
}
//사칙연산
float result = Calculate(1.2f, 5.2f, "+");
Console.WriteLine(result);
//회원가입★
bool isOk = SignUp("email", "password");
Console.WriteLine(isOk);
//스킬연계
string newSkill = LinkSkill("Meteo", "Frost Breath");
Console.WriteLine("{0}가 스킬 연계효과로 발동되었습니다.", newSkill);
//캐릭터 생성
string newCharacter = CreateCharacter("스티브", "나무도끼");
Console.WriteLine(newCharacter);
//변환
string change = ConvertToUpperCase("singed");
Console.WriteLine(change);
//장비 획득
string gottenItem = EquipItem("신지드", "리안드리의 고뇌");
Console.WriteLine(gottenItem);
//enum
eRace race = IntToEnum(2);
Console.WriteLine(race);
}
static private string Synthesize(string itemName1, string itemName2)
{
Console.WriteLine("{0}과 {1}를 합성합니다.", itemName1, itemName2);
string itemName = "강화 도끼";
return itemName;
}
//사칙연산
static private float Calculate(float a, float b, string strOperator)
{
float result = 0;
if (strOperator == "+")
{
result = a + b;
}
return result;
}
//회원가입
static private bool SignUp(string email, string password)
{
bool Email = (bool)true;
bool Password = (bool)false;
if (Email && Password)
{
Console.WriteLine("회원가입을 환영합니다.");
}
else
{
Console.WriteLine("잘못된 정보를 입력했습니다.");
}
return true;
}
//스킬연계
static private string LinkSkill(string skill1, string skill2)
{
return "새로운 스킬'Explosive Wave'";
}
//캐릭터 생성
static private string CreateCharacter(string name, string weaponName)
{
return name + "가 " + weaponName + "를 착용했습니다.";
}
static private string EquipItem(string name, string itemName)
{
return name + "에게 " + itemName + "가 귀속되었습니다.";
}
static private eRace IntToEnum(int num)
{
return (eRace)num;
}
//래핑 메서드 예시
//변환
static private string ConvertToUpperCase(string str)
{
return str.ToUpper() + "123";
}
static private string Build(int gold)
{
if (gold > 150)
{
return "배럭스";
}
else
{
return "";
}
}
}
}
메서드 (반환타입o/매개변수o)
'C# > 수업내용' 카테고리의 다른 글
C# 21.03.12.수업내용 (0) | 2021.03.12 |
---|---|
C# 21.03.11.수업내용 (0) | 2021.03.11 |
C# 0309 하템 스톰사용 (0) | 2021.03.09 |
C# 0309 디팟짓기 (0) | 2021.03.09 |
C# 0309 마린vs저글링 (0) | 2021.03.09 |