Vienna
3일차 본문
오늘도 어디 해 보실까
1. 실습_ArithmaticOperators
using System;
namespace _4장_ArithmaticOperators_119page
{
class Program
{
static void Main(string[] args)
{
int a = 111 + 222;
Console.WriteLine($"a: {a}");
int b = a - 100;
Console.WriteLine($"b: {b}");
int c = b * 10;
Console.WriteLine($"c: {c}");
double d = c / 6.3;
Console.WriteLine($"d: {d}");
Console.WriteLine($"22/7={22 / 7}({22 % 7})");
}
}
}
2. 실습_IncDecOperator
using System;
namespace _4장_IncDecOperator_121page
{
class Program
{
static void Main(string[] args)
{
int a = 10;
Console.WriteLine(a++);
Console.WriteLine(++a);
Console.WriteLine(a--);
Console.WriteLine(--a);
}
}
}
3. 실습_StringConcatenate
using System;
namespace _4장_StringConcatenate_123page
{
class Program
{
static void Main(string[] args)
{
string result = "123" + "456";
Console.WriteLine(result);
result = "Hello" + " " + "World!";
Console.WriteLine(result);
}
}
}
4. 실습_RelationalOperator
using System;
namespace _4장_RelationalOperator_124page
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine($"3>4: {3>4}");
Console.WriteLine($"3>=4: {3 >= 4}");
Console.WriteLine($"3<4: {3 < 4}");
Console.WriteLine($"3<=4: {3 <= 4}");
Console.WriteLine($"3==4: {3 == 4}");
Console.WriteLine($"3!=4: {3 != 4}");
}
}
}
5. 실습_LogicalOperator
using System;
namespace _4장_LogicalOperator_126page
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Testing && ...");
Console.WriteLine($"1>0 && 4<5:{1 > 0 && 4 < 5}");
Console.WriteLine($"1>0 && 4>5:{1 > 0 && 4 > 5}");
Console.WriteLine($"1==0 && 4>5:{1 == 0 && 4 > 5}");
Console.WriteLine($"1==0 && 4<5:{1 == 0 && 4 < 5}");
Console.WriteLine("\nTesting || ...");
Console.WriteLine($"1>0 || 4<5:{1 > 0 || 4 < 5}");
Console.WriteLine($"1>0 || 4>5:{1 > 0 || 4 > 5}");
Console.WriteLine($"1==0 || 4>5:{1 == 0 || 4 > 5}");
Console.WriteLine($"1==0 || 4<5:{1 == 0 || 4 < 5}");
Console.WriteLine("\nTesting ! ...");
Console.WriteLine($"!True: {!true}");
Console.WriteLine($"!False: {!false}");
}
}
}
6. 실습_ConditionalOperator
using System;
namespace _4장_ConditionalOperator_128page
{
class Program
{
static void Main(string[] args)
{
string result = (10 % 2) == 0 ? "짝수" : "홀수";
Console.WriteLine(result);
}
}
}
7. 실습_NullConditionalOperator
using static System.Console;
using System.Collections;
namespace _4장_NullConditionalOperator_130page
{
class Program
{
static void Main(string[] args)
{
ArrayList a = null;
a?.Add("야구"); // a가 null을 반환하므로 Add()는 호출되지 않음
a?.Add("축구");
WriteLine($"Count: {a?.Count}");
WriteLine($"{a?[0]}");
WriteLine($"{a?[1]}");
a = new ArrayList(); // a는 더이상 null상태가 아님
a?.Add("야구");
a?.Add("축구");
WriteLine($"Count: {a?.Count}");
WriteLine($"{a?[0]}");
WriteLine($"{a?[1]}");
}
}
}
8. 실습_ShiftOperator
using System;
namespace _4장_ShiftOperator_134page
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Testing <<...");
int a = 1;
Console.WriteLine("a :{0:D5} (0x{0:X8})", a);
Console.WriteLine("a << 1:{0:D5} (0x{0:X8})", a << 1);
Console.WriteLine("a << 2:{0:D5} (0x{0:X8})", a << 2);
Console.WriteLine("a << 5:{0:D5} (0x{0:X8})", a << 5);
Console.WriteLine("\nTesting >>...");
int b = 255;
Console.WriteLine("b :{0:D5} (0x{0:X8})", b);
Console.WriteLine("b >> 1:{0:D5} (0x{0:X8})", b >> 1);
Console.WriteLine("b >> 2:{0:D5} (0x{0:X8})", b >> 2);
Console.WriteLine("b >> 5:{0:D5} (0x{0:X8})", b >> 5);
Console.WriteLine("\nTesting >> 2...");
int c = -255;
Console.WriteLine("c :{0:D5} (0x{0:X8})", c);
Console.WriteLine("c >> 1:{0:D5} (0x{0:X8})", c >> 1);
Console.WriteLine("c >> 2:{0:D5} (0x{0:X8})", c >> 2);
Console.WriteLine("c >> 5:{0:D5} (0x{0:X8})", c >> 5);
}
}
}
9. 실습_BitwiseOperator
using System;
namespace _4장_BitwiseOperator_138page
{
class Program
{
static void Main(string[] args)
{
int a = 9;
int b = 10;
Console.WriteLine($"{a} & {b}: {a & b}");
Console.WriteLine($"{a} | {b}: {a | b}");
Console.WriteLine($"{a} ^ {b}: {a ^ b}");
int c = 255;
Console.WriteLine("~{0}(0x{0:X8}): {1}(0x{1:X8})", c, ~c);
}
}
}
10. 실습_AssignmentOperator
using System;
namespace _4장_AssignmentOperator_140page
{
class Program
{
static void Main(string[] args)
{
int a;
a = 100;
Console.WriteLine($"a=100: {a}");
a += 90;
Console.WriteLine($"a+=90: {a}");
a -= 80;
Console.WriteLine($"a-=80: {a}");
a *= 70;
Console.WriteLine($"a*=70: {a}");
a /= 60;
Console.WriteLine($"a/=60: {a}");
a %= 50;
Console.WriteLine($"a%=50: {a}");
a &= 40;
Console.WriteLine($"a&=40: {a}");
a |= 30;
Console.WriteLine($"a|=30: {a}");
a ^= 20;
Console.WriteLine($"a^=20: {a}");
a <<= 10;
Console.WriteLine($"a<<=10: {a}");
a >>= 1;
Console.WriteLine($"a>>=1: {a}");
}
}
}
11. 실습_NullCoalescing
using System;
namespace _4장_NullCoalescing_142page
{
class Program
{
static void Main(string[] args)
{
int? num = null;
Console.WriteLine($"{num ?? 0}");
num = 99;
Console.WriteLine($"{num ?? 0}");
string str = null;
Console.WriteLine($"{str ?? "Default"}");
str = "Sepecific";
Console.WriteLine($"{str ?? "Default"}");
}
}
}
12. 실습_IfElse
using System;
namespace _5장_IfElse_149page
{
class Program
{
static void Main(string[] args)
{
Console.Write("숫자를 입력하세요: ");
string input = Console.ReadLine();
int number = Int32.Parse(input);
if (number < 0)
Console.WriteLine("음수");
else if(number >0)
Console.WriteLine("양수");
else
Console.WriteLine("0");
if ((number % 2) == 0)
Console.WriteLine("짝수");
else
Console.WriteLine("홀수");
}
}
}
13. 실습_IfIf
using System;
namespace _5장_IfIf_151page
{
class Program
{
static void Main(string[] args)
{
Console.Write("숫자를 입력하세요: ");
string input = Console.ReadLine();
int number = Int32.Parse(input);
if (number > 0)
{
if ((number % 2) == 0)
Console.WriteLine("0보다 큰 짝수");
else
Console.WriteLine("0보다 큰 홀수");
}
else
Console.WriteLine("0보다 작거나 같은 수");
}
}
}
14. 실습_Switch
using System;
namespace _5장_Switch_152page
{
class Program
{
static void Main(string[] args)
{
Console.Write("요일을 입력하세요.(일,월,화,수,목,금,토): ");
string day = Console.ReadLine();
switch (day) {
case "일": Console.WriteLine("Sunday"); break;
case "월": Console.WriteLine("Monday"); break;
case "화": Console.WriteLine("Tuesday"); break;
case "수": Console.WriteLine("Wednsday"); break;
case "목": Console.WriteLine("Thursday"); break;
case "금": Console.WriteLine("Friday"); break;
case "토": Console.WriteLine("Saturday"); break;
default: Console.WriteLine($"{day}는(은) 요일이 아닙니다."); break;
}
}
}
}
15. 실습_Switch2
using System;
namespace _5장_Switch2_156page
{
class Program
{
static void Main(string[] args)
{
object obj = null;
string s = Console.ReadLine();
if (int.TryParse(s, out int out_i))
obj = out_i;
else if (float.TryParse(s, out float out_f))
obj = out_f;
else
obj = s;
switch (obj) {
case int i:
Console.WriteLine($"{i}는 int 형식입니다.");
break;
case float f:
Console.WriteLine($"{f}는 float 형식입니다.");
break;
default:
Console.WriteLine($"{obj}는 모르는 형식입니다.");
break;
}
}
}
}
16. 실습_SwitchExp
using System;
namespace _5장_SwitchExp_161page
{
class Program
{
static void Main(string[] args)
{
Console.Write("점수를 입력하세요: ");
int score = Convert.ToInt32(Console.ReadLine());
Console.Write("재수강인가요? (y/n): ");
string line = Console.ReadLine();
bool repeated = line == "y" ? true : false;
string grade = (int)(Math.Truncate(score / 10.0) * 10) switch
{
90 when repeated == true => "B+",
90 => "A",
80 => "B",
70 => "C",
60 => "D",
_ => "F"
};
Console.WriteLine($"학점: {grade}");
}
}
}
짱이다...
17. 실습_While
using System;
namespace _5장_While_163page
{
class Program
{
static void Main(string[] args)
{
int i = 10;
while (i > 0)
Console.WriteLine($"i: { i--}");
}
}
}
18. 실습_DoWhile
using System;
namespace _5장_DoWhile_166page
{
class Program
{
static void Main(string[] args)
{
int i = 10;
do
{
Console.WriteLine("a) i: {0}", i--);
} while (i > 0);
do
{
Console.WriteLine("b) i: {0}", i--);
} while (i > 0);
}
}
}
19. 실습_For
using System;
namespace _5장_For_168page
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 5; i++)
{
Console.WriteLine(i);
}
}
}
}
20. 실습_ForFor
using System;
namespace _5장_ForFor_169page
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 10; j++)
Console.Write("*");
Console.WriteLine();
}
}
}
}
21. 실습_ForEach
using System;
namespace _5장_ForEach_171page
{
class Program
{
static void Main(string[] args)
{
int[] arr = new int[] { 0, 1, 2, 3, 4 };
foreach(int a in arr)
Console.WriteLine(a);
}
}
}
22. 실습_InfiniteFor
using System;
namespace _5장_InfiniteFor_173page
{
class Program
{
static void Main(string[] args)
{
int i = 0;
for (; ; )
Console.WriteLine(i++);
}
}
}
23. 실습_InfiniteWhile
using System;
namespace _5장_InfiniteWhile_173page
{
class Program
{
static void Main(string[] args)
{
int i = 0;
while(true)
Console.WriteLine(i++);
}
}
}
24. 실습_Break
using System;
namespace _5장_Break_175page
{
class Program
{
static void Main(string[] args)
{
while(true)
{
Console.Write("계속할까요? (예/아니오): ");
string answer = Console.ReadLine();
if (answer == "아니오") break;
}
}
}
}
25. 실습_Continue
using System;
namespace _5장_Continue_176page
{
class Program
{
static void Main(string[] args)
{
for(int i=0; i < 10;i++)
{
if (i % 2 == 0)
continue;
Console.WriteLine($"{i}: 홀수");
}
}
}
}
26. 실습_Goto
using System;
namespace _5장_Goto_179page
{
class Program
{
static void Main(string[] args)
{
Console.Write("종료 조건(숫자)을 입력하세요: ");
String input = Console.ReadLine();
int input_number = Convert.ToInt32(input);
int exit_number = 0;
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
for(int k=0;k<3;k++)
{
if (exit_number++ == input_number)
goto EXIT_FOR;
Console.WriteLine(exit_number);
}
}
}
goto EXIT_PROGRAM;
EXIT_FOR:
Console.WriteLine("\nExit nested for...");
EXIT_PROGRAM:
Console.WriteLine("\nExit program...");
}
}
}
27. 연습문제1
using System;
namespace 연습문제1_182page
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < i+1; j++)
Console.Write("*");
Console.WriteLine();
}
}
}
}
28. 연습문제2
using System;
namespace 연습문제2_182page
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 5; i++)
{
for (int j = 5 - i; j > 0; j--)
Console.Write("*");
Console.WriteLine();
}
}
}
}
29. 연습문제3
using System;
namespace 연습문제3_182page
{
class Program
{
static void Main(string[] args)
{
int i = 0, j=0;
while(i<5)
{
Console.Write("*");
if(++j > i)
{
i++;
Console.WriteLine();
j = 0;
}
}
Console.WriteLine();
i = 0;
j = 0;
do {
Console.Write("*");
j++;
if (5-j <= i)
{
j = 0;
Console.WriteLine();
i++;
}
} while (i < 5);
}
}
}
30. 연습문제4
using System;
namespace 연습문제4_182page
{
class Program
{
static void Main(string[] args)
{
int num = 0;
do
{
Console.Write("반복 횟수를 입력하세요 : ");
num = Convert.ToInt32(Console.ReadLine());
if (num <= 0) Console.WriteLine("0보다 작거나 같은 수는 입력할 수 없습니다.\n");
} while (num<=0);
Console.WriteLine();
for (int i = 0; i < num; i++)
{
for (int j = 0; j < i + 1; j++)
Console.Write("*");
Console.WriteLine();
}
}
}
}
Comments