דוגמאות ללולאת while ב c#:כתוב תכנית אשר קולטת מספ...
דוגמאות ללולאת while ב c#:
כתוב תכנית אשר קולטת מספר, עד אשר נקלט מספר שלילי:
Console.WriteLine("Please enter int number:");
int x = int.Parse(Console.ReadLine());
while(x>=0) // כל עוד המספר חיובי או אפס המשך לקלוט
{
Console.WriteLine("Please enter int number:");
x = int.Parse(Console.ReadLine());
}
ברגע שהמספר יהיה שלילי נצא מלולאת ה-while.
Another exmaple:
Create program which get grade (int) from user. if the grade is illegal, repeat and continue to get grade from user.. and if the grade is legal, print: "Thanks".
solution:
Console.WriteLine("Please enter int grade:");
int grade = int.Parse(Console.ReadLine());
while( grade<0 || grade>100 )
{
Console.WriteLine("Please enter int grade:");
grade = int.Parse(Console.ReadLine());
}
Console.WriteLine("Thanks");
More example for using while:
create function that get string, and count how many char 'a' exists until the first space.
Any string has one space at least.
for example:
"abbaacsd asdae" has 3 times 'a' before the first space.