본문 바로가기

언어28

[C#] while문 (while loop) using System; namespace MyFirstProgram { internal class Program { static void Main(string[] args) // main method { // while String my_new_name = ""; while (my_new_name == "") // 괄호 안의 조건이 true일경우 반복 { Console.Write("Enter your name: "); my_new_name = Console.ReadLine(); } } } } 2023. 3. 1.
[C#] 논리연산자 && (and), || (or) using System; namespace MyFirstProgram { internal class Program { static void Main(string[] args) // main method { // 논리연산자 // && and, || or Console.WriteLine("What's the temperature outside: (C)"); double temp = Convert.ToDouble(Console.ReadLine()); if (temp >= 10 && temp 2023. 3. 1.
[C#] switch 문 switch - case using System; namespace MyFirstProgram { internal class Program { static void Main(string[] args) // main method { // switch - case Console.WriteLine("What day is it today?"); String day = Console.ReadLine(); switch (day) // day에 해당하는 case로 이동하여 코드를 실행한다 { case "Monday": Console.WriteLine("It's Monday!"); break; case "Tuesday": Console.WriteLine("It's Tuesday!"); break; case "Wednesday": Console.. 2023. 3. 1.
[C#] if 조건문 if - else if - else using System; namespace MyFirstProgram { internal class Program { static void Main(string[] args) // main method { // if 조건문 Console.WriteLine("Please enter your age: "); int myAge = Convert.ToInt32(Console.ReadLine()); if (myAge > 100) { Console.WriteLine("You are too old to sign up!"); } else if (myAge = 18) { Console.Wr.. 2023. 3. 1.
[C#] String 함수 ToUpper(), ToLower(), Replace(), Insert(), Length, Substring() using System; namespace MyFirstProgram { internal class Program { static void Main(string[] args) // main method { // String method String fullName = "Jo Lab"; // fullName = fullName.ToUpper(); // 모든 문자를 대문자로 변경 // fullName = fullName.ToLower(); // 모든 문자를 소문자로 변경 Console.WriteLine(fullName); String numBer = "123-456-7890"; numBer = numBer.Replace("-", "/"); // 앞의 문자열을 뒤의 문자열로 변경 Console.WriteLin.. 2023. 3. 1.
[C#] 피타고라스 정리 (hypotenuse) using System; namespace MyFirstProgram { internal class Program { static void Main(string[] args) // main method { // 피타고라스 Console.WriteLine("Enter side A: "); double a = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("Enter side B: "); double b = Convert.ToDouble(Console.ReadLine()); double c = Math.Sqrt((a * a) + (b * b)); Console.WriteLine("The hypotenuse is : " + c); Console.ReadKe.. 2023. 3. 1.
[C#] 랜덤 Random (Random.Next(), Random.NextDouble()) using System; namespace MyFirstProgram { internal class Program { static void Main(string[] args) // main method { Random random = new Random(); Console.WriteLine(random.Next() % 100 + 1); // 정수: 범위 설정 방법 1 Console.WriteLine(random.Next(1, 11)); // 정수: 범위 설정 방법 2 Console.WriteLine(random.NextDouble() * 100 + 1); // 실수 } } } 2023. 2. 25.
[C#] Math 클래스 (Pow, Sqrt, Abs, Round, Ceiling, Floor, Max, Min) using System; namespace MyFirstProgram { internal class Program { static void Main(string[] args) // main method { double xx = 3; double xxx = Math.Pow(xx, 4); Console.WriteLine(xxx); // 81 double yy = Math.Sqrt(Math.Sqrt(xxx)); Console.WriteLine(yy); // 9 double cc = Math.Abs(-123); Console.WriteLine(cc); // 123 double dd = Math.Round(3.14); Console.WriteLine(dd); // 3 double ee = Math.Ceiling(.. 2023. 2. 25.
[C#] 연산자 +, -, *, /, ++, +=, -- -=, *=, /= using System; namespace MyFirstProgram { internal class Program { static void Main(string[] args) // main method { int friends = 5; friends = friends + 1; // 6 friends++; // 1씩 증가해줌 -> 7 friends += 2; // 뒤에 적은 숫자만큼 증가해줌 -> 9 Console.WriteLine(friends); int friends = 5; friends = friends - 1; // 4 friends--; // 1씩 감소해줌 -> 3 friends -= 2; // 뒤에 적은 숫자만큼 감소해줌 -> 1 Console.WriteLine(friends); int fri.. 2023. 2. 25.
[C#] 기본 입력 Console.ReadLine() using System; using System.Runtime.InteropServices; namespace MyFirstProgram { internal class Program { static void Main(string[] args) // main method { // 기본적으로 string으로 입력됨 // int, double, boolean, char 등으로 사용하기 위해서는 타입캐스팅이 필요함 string example = Console.ReadLine(); Console.WriteLine(example); } } } 2023. 2. 25.
[C#] 함수 설명 method summary using System; namespace MyFirstProgram { internal class Program { /// /// _name을 출력해주는 함수 /// /// _name에 아무 string이나 입력하세요. static void Printname(string _name) { Console.WriteLine(_name); } static void Main(string[] args) // main method { string para = Console.ReadLine(); Printname(para); } } } 2023. 2. 25.
[C#] 타입캐스트 TypeCasting (Convert class) using System; namespace MyFirstProgram { internal class Program { static void Main(string[] args) // main method { double x = 3.14; int x_2 = Convert.ToInt32(x); Console.WriteLine(x.GetType()); Console.WriteLine(x_2.GetType()); double d = Convert.ToDouble(x_2) + 0.14; Console.WriteLine(d.GetType()); // int e = 321; double e = 312.123; String f = Convert.ToString(e); Console.WriteLine(f.GetType().. 2023. 2. 25.
728x90