Programming/C# - Window
C#/ Switch 식
esoog Polaris
2023. 6. 27. 11:36
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"); | |
} | |
} | |
} |
반응형