1769
צפיות
צפיות
1
תשובות
תשובות
קפיצות ב C#
אני כתבתי תוכנית ב C# וזה לא עובד מישהו יכול להסביר לי למה
int counter = 0;
Console.WriteLine("please enter a name");
string name = Console.ReadLine();
while (name!= "gg")
{
Console.WriteLine("please enter a name");
if (name == "anat")
{
Console.WriteLine("you cool");
counter++;
}
else
Console.WriteLine("you are not cool");
}
Console.WriteLine("the numbers of anat is"+ counter)
1 תשובות
יש פה לופ אינסופי מכיוון שהתנאי של ה- while תמיד יהיה נכון(כל עוד לא הוקש gg).
בלופ הזה אין אפילו עצירה בשביל לקבל קלט חדש מהמשתמש, ובגלל זה רץ בלי הפסקה.
גם לא בטוח שהבנתי את הרעיון של לבקש פעמיים להקיש שם, אבל זרמתי עם זה והכנתי דוגמא שעובדת:
int counter = 0;
Console.WriteLine("please enter a name");
string name = Console.ReadLine();
while (name != "gg")
{
Console.WriteLine("please enter a name");
string newName = Console.ReadLine();
if (newName == "anat")
{
Console.WriteLine("you cool");
counter++;
}
else
{
Console.WriteLine("you are not cool");
break;
}
}
Console.WriteLine("the numbers of anat is" + counter);