47 lines
1.2 KiB
C#
47 lines
1.2 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Collections.Generic;
|
|
|
|
namespace Exercises_C_Sharp.E19_Überladung
|
|
{
|
|
class Exercise_4
|
|
{
|
|
//Sie sollen die Methode ConvertToInt so schreiben, dass mit allen drei Aufrufen der richtige Integer-Wert zurück gegeben wird. Verwenden Sie dafür Überladung und kein dynamic.
|
|
|
|
public static void Start()
|
|
{
|
|
Console.WriteLine(ConvertToInt('7'));
|
|
Console.WriteLine(ConvertToInt("Drei"));
|
|
Console.WriteLine(ConvertToInt("i"));
|
|
}
|
|
|
|
//Code START
|
|
static int ConvertToInt(char d)
|
|
{
|
|
//Lösung 1:
|
|
return (int)char.GetNumericValue(d);
|
|
|
|
//Lösung 2:
|
|
return Convert.ToInt32(d.ToString());
|
|
|
|
//Lösung 3:
|
|
return d;
|
|
}
|
|
|
|
static int ConvertToInt(string s)
|
|
{
|
|
//Lösung 1:
|
|
int i = 0;
|
|
foreach(var element in s)
|
|
i += element;
|
|
return i;
|
|
|
|
//Lösung 2:
|
|
if(s == "Eins") return 1;
|
|
else if(s == "Zwei") return 2;
|
|
else if(s == "Drei") return 3;
|
|
//...
|
|
}
|
|
//Code ENDE
|
|
}
|
|
} |