Difference between revisions of "C Sharp"
From Teknologisk videncenter
| (12 intermediate revisions by the same user not shown) | |||
| Line 15: | Line 15: | ||
static void Main(string[] args) | static void Main(string[] args) | ||
{ | { | ||
| − | |||
string ope = Console.ReadLine(); | string ope = Console.ReadLine(); | ||
| − | + | ||
if (ope == "+") | if (ope == "+") | ||
{ Plus(); } | { Plus(); } | ||
| Line 24: | Line 23: | ||
else | else | ||
{ Console.WriteLine("det var ikke en gyldig operator"); } | { Console.WriteLine("det var ikke en gyldig operator"); } | ||
| − | + | ||
| − | |||
| − | |||
Console.ReadLine(); | Console.ReadLine(); | ||
} | } | ||
| − | + | ||
static void Plus() | static void Plus() | ||
{ | { | ||
| Line 36: | Line 33: | ||
Console.WriteLine(tal1 + tal2); | Console.WriteLine(tal1 + tal2); | ||
} | } | ||
| − | + | ||
static void minus() | static void minus() | ||
{ | { | ||
| Line 43: | Line 40: | ||
Console.WriteLine(tal1 - tal2); | Console.WriteLine(tal1 - tal2); | ||
} | } | ||
| + | |||
| + | ''' loop's ''' | ||
| + | int counter = 0; | ||
| + | while(counter < 100) | ||
| + | { | ||
| + | Console.WriteLine(counter); | ||
| + | } | ||
| + | |||
| + | int counter = 200; | ||
| + | string test = "hello world"; | ||
| + | |||
| + | foreach (char i in test) | ||
| + | { | ||
| + | Console.WriteLine(i); | ||
| + | } | ||
| + | |||
| + | |||
| + | '''Subnet calculator''' | ||
| + | class Program | ||
| + | { | ||
| + | static string claculateSub(int prefix2) | ||
| + | { | ||
| + | string Sub = ""; | ||
| + | |||
| + | if (prefix2 > 7) | ||
| + | { | ||
| + | Sub = "11111111"; | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | for (int i = 0; i < prefix2; i++) | ||
| + | { | ||
| + | Sub = Sub + "1"; | ||
| + | } | ||
| + | |||
| + | while (Sub.Length < 8) | ||
| + | { | ||
| + | Sub = Sub + "0"; | ||
| + | } | ||
| + | } | ||
| + | return Sub; | ||
| + | } | ||
| + | |||
| + | static void Main(string[] args) | ||
| + | { | ||
| + | Console.WriteLine("Skriv subnetets prefix: "); | ||
| + | int prefix = Convert.ToInt32(Console.ReadLine()); | ||
| + | |||
| + | int Sub1 = 0; | ||
| + | int Sub2 = 0; | ||
| + | int Sub3 = 0; | ||
| + | int Sub4 = 0; | ||
| + | |||
| + | Sub1 = Convert.ToInt32(claculateSub(prefix),2); | ||
| + | prefix = prefix - 8; | ||
| + | Sub2 = Convert.ToInt32(claculateSub(prefix),2); | ||
| + | prefix = prefix - 8; | ||
| + | Sub3 = Convert.ToInt32(claculateSub(prefix),2); | ||
| + | prefix = prefix - 8; | ||
| + | Sub4 = Convert.ToInt32(claculateSub(prefix),2); | ||
| + | |||
| + | Console.WriteLine(Sub1 + "." + Sub2 + "." + Sub3 + "." + Sub4); | ||
| + | Console.WriteLine(prefix); | ||
| + | |||
| + | Console.ReadLine(); | ||
| + | } | ||
Latest revision as of 14:54, 6 December 2016
Kode eksempler - Gentelman klub
Console.WriteLine("Skriv din alder");
string alder = Console.ReadLine();
int age;
age = Convert.ToInt32(alder);
if (((age > 20) && (age < 80)) || age != 21)
{
Console.WriteLine("Velkommen!");
}
else { Console.WriteLine("gå hjem og spil OverWatch"); }
Console.ReadLine();
static void Main(string[] args)
{
string ope = Console.ReadLine();
if (ope == "+")
{ Plus(); }
else if (ope == "-")
{ minus(); }
else
{ Console.WriteLine("det var ikke en gyldig operator"); }
Console.ReadLine();
}
static void Plus()
{
int tal1 = Convert.ToInt32(Console.ReadLine());
int tal2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(tal1 + tal2);
}
static void minus()
{
int tal1 = Convert.ToInt32(Console.ReadLine());
int tal2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(tal1 - tal2);
}
loop's
int counter = 0;
while(counter < 100)
{
Console.WriteLine(counter);
}
int counter = 200;
string test = "hello world";
foreach (char i in test)
{
Console.WriteLine(i);
}
Subnet calculator
class Program
{
static string claculateSub(int prefix2)
{
string Sub = "";
if (prefix2 > 7)
{
Sub = "11111111";
}
else
{
for (int i = 0; i < prefix2; i++)
{
Sub = Sub + "1";
}
while (Sub.Length < 8)
{
Sub = Sub + "0";
}
}
return Sub;
}
static void Main(string[] args)
{
Console.WriteLine("Skriv subnetets prefix: ");
int prefix = Convert.ToInt32(Console.ReadLine());
int Sub1 = 0;
int Sub2 = 0;
int Sub3 = 0;
int Sub4 = 0;
Sub1 = Convert.ToInt32(claculateSub(prefix),2);
prefix = prefix - 8;
Sub2 = Convert.ToInt32(claculateSub(prefix),2);
prefix = prefix - 8;
Sub3 = Convert.ToInt32(claculateSub(prefix),2);
prefix = prefix - 8;
Sub4 = Convert.ToInt32(claculateSub(prefix),2);
Console.WriteLine(Sub1 + "." + Sub2 + "." + Sub3 + "." + Sub4);
Console.WriteLine(prefix);
Console.ReadLine();
}