Wie würden Sie die Eingabe des Benutzers auf elegantere Weise überprüfen?

Post a reply

Smilies
:) :( :oops: :chelo: :roll: :wink: :muza: :sorry: :angel: :read: *x) :clever:
View more smilies

BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Wie würden Sie die Eingabe des Benutzers auf elegantere Weise überprüfen?

by Anonymous » 14 Apr 2025, 22:03

Ich habe zwei Listen erstellt, um die Antworten "Ja, ja, nein" und "Nein" zu überprüfen.

Code: Select all

Console.WriteLine("Please enter your name.");
string name = Console.ReadLine();

Console.WriteLine("Now, enter your age");
int age = Int32.Parse(Console.ReadLine());

Console.WriteLine("Enter your height in cm's.");
int heightCm = Int32.Parse(Console.ReadLine());

Console.WriteLine("Lastly. Are you a student?");
string studentInput = Console.ReadLine();

string studentAnswer = "Placeholder";

string[] studentListYes = new string[2];
studentListYes[0] = "Yes";
studentListYes[1] = "yes";

string[] studentListNo = new string[2];
studentListNo[0] = "No";
studentListNo[1] = "no";

if (studentListYes.Contains(studentInput))
{
studentAnswer = "you are a student.";
}

else if (studentListNo.Contains(studentInput))
{
studentAnswer = "you are not a student.";
}

else
{
Console.WriteLine("That is not a valid input, try again.");
}

Console.WriteLine($"Your name is {name}, you are {age} years old, and {studentAnswer}");

Top