Programming/C# - Window

C#/ Switch 식

esoog Polaris 2023. 6. 27. 11:36
using System;
namespace SwitchExp
{
class MainApp
{
static void Main(string[] args)
{
Console.WriteLine("점수를 입력하세요");
int score = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("재수강인가요? (y/n)");
string line = Console.ReadLine();
bool repeated = line == "y"?true:false;
string grade = (int)(Math.Truncate(score/10.0) * 10) switch
// 위치 유의!
// 보통 switch case문으로 사용하나,
// switch 식으로 바꿔 사용 가능. 더 간편.
// switch{여기 안에 식으로 채운다. => 문법 사용}
{
90 when repeated == true => "B+",
// when 은 조건절이다. 서브로 사용하면 더 구체적으로 조건을 만들 수 있다.
// => 문법은 (then)로 보면 된다.
90 => "A",
80 => "B",
70 => "C",
60 => "D",
_ => "F"
// _ 언더바는 default 키워드와 같다.
};
Console.WriteLine($"학점 : {grade}");
}
}
}
view raw MainApp.cs hosted with ❤ by GitHub
반응형

'Programming > C# - Window' 카테고리의 다른 글

C#/ 한정자  (0) 2023.06.27
C#/ for 반복문  (0) 2023.06.27
C#/ Null 연산자  (0) 2023.06.27
C#/ 문자열 서식  (1) 2023.06.27
C#/ var  (0) 2023.06.27