E18 Ex 5 + 6
This commit is contained in:
parent
25d48a6946
commit
9021010b8d
@ -6,12 +6,48 @@ namespace Exercises_C_Sharp.E18_Methods
|
||||
{
|
||||
//2. Fragen Sie den Nutzer mit Hilfe der unten erstellten Methode nach einer Primzahl. Fragen Sie diesen dann, ob er noch eine weitere Primzahl eingeben möchte. Wiederholen Sie den Vorgang, bis der Nutzer keine Zahl mehr eingeben möchte. Multiplizieren Sie dann alle Eingaben miteinander und geben das Ergebnis aus.
|
||||
//Code START
|
||||
|
||||
int sum = 1;
|
||||
while(true)
|
||||
{
|
||||
sum *= AskUserForPrim();
|
||||
Console.WriteLine("Möchten Sie eine weitere Primzahl eingeben? Y = ja");
|
||||
if(Console.ReadLine() != "Y") break;
|
||||
}
|
||||
Console.WriteLine("Summe der Multiplikation: " + sum);
|
||||
//Code ENDE
|
||||
}
|
||||
//Schreiben Sie eine Methode, bei der der Nutzer eine Primzahl (Zahl, die nur durch 1 und sich selbst teilbar ist) eingeben soll. Geben Sie diese Zahl zurück. Wenn der Nutzer eine falsche Eingabe tätigt, dann soll der Nutzer darüber informiert und die Abfrage wiederholt werden.
|
||||
//Code START
|
||||
|
||||
public static int AskUserForPrim()
|
||||
{
|
||||
while(true)
|
||||
{
|
||||
int userinput;
|
||||
//Usereingabe / Prüfung auf valide Zahl
|
||||
if(!int.TryParse(Console.ReadLine(), out userinput))
|
||||
{
|
||||
Console.WriteLine("Falsche Eingabe!");
|
||||
continue;
|
||||
}
|
||||
|
||||
//Prüfung auf Primzahl
|
||||
bool isPrim = true;
|
||||
for(int i = 2; i < userinput; i++)
|
||||
{
|
||||
if(userinput % i == 0)
|
||||
{
|
||||
//Keine Primzahl
|
||||
Console.WriteLine("Es handelt sich nicht um eine Primzahl");
|
||||
isPrim = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
//Keine Primzahl -> von Vorne
|
||||
if(!isPrim) continue;
|
||||
|
||||
return userinput;
|
||||
}
|
||||
}
|
||||
//Code ENDE
|
||||
|
||||
}
|
||||
|
@ -16,42 +16,18 @@ namespace Exercises_C_Sharp.E18_Methods
|
||||
{
|
||||
//Ausgabe der Anzeige für den User
|
||||
//Code START
|
||||
Console.Clear();
|
||||
foreach(var element in word)
|
||||
{
|
||||
if(InputList.Contains(element))
|
||||
Console.Write(element);
|
||||
else
|
||||
Console.Write("_");
|
||||
}
|
||||
Console.WriteLine();
|
||||
ShowWord(word);
|
||||
//Code ENDE
|
||||
|
||||
//Fragen nach der Eingabe
|
||||
//Code START
|
||||
Console.WriteLine("Bitte geben Sie einen Buchstaben ein:");
|
||||
while(true)
|
||||
{
|
||||
string userinput = (Console.ReadLine() ?? "").ToUpper();
|
||||
if(userinput == null || userinput.Length == 0 || InputList.Contains(userinput[0]))
|
||||
continue;
|
||||
InputList.Add(userinput[0]);
|
||||
break;
|
||||
}
|
||||
AskUserForInput();
|
||||
//Code ENDE
|
||||
tries++;
|
||||
|
||||
//Kontrolle, ob alle Elemente gefunden wurden
|
||||
//Code START
|
||||
bool allFound = true;
|
||||
foreach(var element in word)
|
||||
{
|
||||
if(!InputList.Contains(element))
|
||||
{
|
||||
allFound = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
bool allFound = TestAllFound(word);
|
||||
//Code ENDE
|
||||
if(allFound)
|
||||
break;
|
||||
@ -65,6 +41,48 @@ namespace Exercises_C_Sharp.E18_Methods
|
||||
|
||||
//Hier kommen die Methoden hin:
|
||||
//Code START
|
||||
|
||||
#region METHODEN
|
||||
|
||||
public static void ShowWord(string word)
|
||||
{
|
||||
Console.Clear();
|
||||
foreach(var element in word)
|
||||
{
|
||||
if(InputList.Contains(element))
|
||||
Console.Write(element);
|
||||
else
|
||||
Console.Write("_");
|
||||
}
|
||||
Console.WriteLine();
|
||||
}
|
||||
public static void AskUserForInput()
|
||||
{
|
||||
Console.WriteLine("Bitte geben Sie einen Buchstaben ein:");
|
||||
while(true)
|
||||
{
|
||||
string userinput = (Console.ReadLine() ?? "").ToUpper();
|
||||
if(userinput == null || userinput.Length == 0 || InputList.Contains(userinput[0]))
|
||||
continue;
|
||||
InputList.Add(userinput[0]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
public static bool TestAllFound(string word)
|
||||
{
|
||||
bool allFound = true;
|
||||
foreach(var element in word)
|
||||
{
|
||||
if(!InputList.Contains(element))
|
||||
{
|
||||
allFound = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return allFound;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
//Code ENDE
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user