שלח תשובה

זירת השאלות

1769
צפיות
1
תשובות

קפיצות ב C#

,‏ 12 בנובמבר, 2015

אני כתבתי תוכנית ב 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 תשובות

  1. nadavkrv הגיב:

    יש פה לופ אינסופי מכיוון שהתנאי של ה- 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);

שלח תשובה