Skip to content

Commit fffeee5

Browse files
committed
add ternary operation exercises
1 parent 4b4dd2a commit fffeee5

File tree

4 files changed

+176
-0
lines changed

4 files changed

+176
-0
lines changed

TernaryOperations/Program.cs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using System.Runtime.CompilerServices;
2+
using System.Security.Cryptography.X509Certificates;
3+
4+
namespace TernaryOperations
5+
{
6+
internal class Program
7+
{
8+
static void Main(string[] args)
9+
{
10+
#region TernaryOperator-test
11+
12+
Console.WriteLine("Hello, World!\nEnter Number : ");
13+
int num = int.Parse(Console.ReadLine());
14+
15+
// ternary operator
16+
// syntax -> variable = (condition) ? (if true) : (if false)
17+
string message = (num % 2 == 0) ? $"Number is EVEN" : $"Number is ODD";
18+
Console.WriteLine($"This is Amazing MeNtAL!\n{message}");
19+
20+
#endregion
21+
22+
23+
#region T16-KUUKAUDET
24+
// T16 - identify season
25+
//using do-while and switch
26+
27+
// initialize month
28+
int month;
29+
30+
// repeat loop until user inputs between 1-12
31+
do
32+
{
33+
Console.WriteLine(@"
34+
----Aloita Tehtävä 16 : Kuukaudet----
35+
Enter month number to identify season : ");
36+
month = int.Parse(Console.ReadLine());
37+
38+
// print based on user input
39+
switch (month)
40+
{
41+
case 11:
42+
case 12:
43+
case 1:
44+
case 2:
45+
case 3:
46+
Console.WriteLine("Talvi Tulossa! Brrr!");
47+
break;
48+
case 4:
49+
case 5:
50+
Console.WriteLine("Keväääääätt...");
51+
break;
52+
case 6:
53+
case 7:
54+
case 8:
55+
Console.WriteLine("Kesääää!!");
56+
break;
57+
case 9:
58+
case 10:
59+
Console.WriteLine("Syksy Nukkuuu...");
60+
break;
61+
default:
62+
Console.WriteLine("\nEnter number between 1 - 12 : ");
63+
break;
64+
}
65+
}
66+
while (month > 12 || month < 1); // repeat if user input invalid
67+
68+
69+
//using class and ternary operator
70+
Console.WriteLine(@"
71+
T16 using ternary operator ""(condition) ? (if true) : (if false) ""
72+
Testing with Ternary.
73+
---Enter a number between 1 - 12 : ");
74+
int monthNum = int.Parse(Console.ReadLine());
75+
string seasonMsg = month >= 11 && month <= 12 ? "Keväääätt..." :
76+
month >= 6 && month <= 10 ? "Kesäää" :
77+
month >= 5 && month <= 1 ? "Tälvi" : "Myös Tälvi";
78+
Console.WriteLine(seasonMsg);
79+
80+
Console.WriteLine("----Loppuu Tehtävä----");
81+
82+
#endregion
83+
}
84+
}
85+
}

TernaryOperations/T16_Kuukaudet.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System;
2+
3+
/// <summary>
4+
/// Summary description for Class1
5+
/// </summary>
6+
public class Class1
7+
{
8+
public Class1()
9+
{
10+
// T16 - identify season
11+
//using do-while and switch
12+
13+
// initialize month
14+
int month;
15+
16+
// repeat loop until user inputs between 1-12
17+
do
18+
{
19+
Console.WriteLine(@"
20+
----Aloita Tehtävä 16 : Kuukaudet----
21+
Enter month number to identify season : ");
22+
month = int.Parse(Console.ReadLine());
23+
24+
// print based on user input
25+
switch (month)
26+
{
27+
case 11:
28+
case 12:
29+
case 1:
30+
case 2:
31+
case 3:
32+
Console.WriteLine("Talvi Tulossa! Brrr!");
33+
break;
34+
case 4:
35+
case 5:
36+
Console.WriteLine("Keväääääätt...");
37+
break;
38+
case 6:
39+
case 7:
40+
case 8:
41+
Console.WriteLine("Kesääää!!");
42+
break;
43+
case 9:
44+
case 10:
45+
Console.WriteLine("Syksy Nukkuuu...");
46+
break;
47+
default:
48+
Console.WriteLine("\nEnter number between 1 - 12 : ");
49+
break;
50+
}
51+
}
52+
while (month > 12 || month < 1); // repeat if user input invalid
53+
54+
Console.WriteLine("---Loppuu Tehtävä---");
55+
}
56+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.3.32929.385
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TernaryOperations", "TernaryOperations.csproj", "{F45E4D95-BF3B-44A5-A182-C377591DA885}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{F45E4D95-BF3B-44A5-A182-C377591DA885}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{F45E4D95-BF3B-44A5-A182-C377591DA885}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{F45E4D95-BF3B-44A5-A182-C377591DA885}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{F45E4D95-BF3B-44A5-A182-C377591DA885}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {D1A63FD8-04C0-4381-854C-351E8219BEA9}
24+
EndGlobalSection
25+
EndGlobal

0 commit comments

Comments
 (0)