簡體   English   中英

C# 計算平均值會增加一個額外的數字

[英]C# calculating an average adds an extra number

我是 C# 的新程序員,在這個網站上是新的,找不到關於這個主題的類似帖子,如果它已經被覆蓋,很抱歉。

作為視頻課程的一部分,我被指示創建一個控制台“APP”來計算學生的平均分數。 用戶輸入學生總數,然后輸入每個人的分數,然后將總和除以學生人數。 我不明白的是為什么 C# 為學生增加了另一個額外的數字? 所以用戶輸入5,我得到6次得分......

class Program
{
 

    static void Main(string[] args)
    {
        
        int totalScores= 0;
        int totalStudents = 0;
        int number;

        Console.WriteLine("how many students are there?");
        int studentNumber = int.Parse(Console.ReadLine());
        

        while(totalStudents <= studentNumber)
        {
            Console.WriteLine("enter your student score");
            string studentScore = Console.ReadLine();
            if (int.TryParse(studentScore, out number))
            {
                totalScores += number;
                totalStudents += 1;
            }
            else
            {
                Console.WriteLine("you did not enter a number");
            }
            
        }
        int avarage = totalScores / totalStudents;

        Console.WriteLine("the average is" + average);
    }

    
}

這是我在控制台中看到的:控制台

我確定我錯過了什么……有什么建議嗎?

迭代器變量“ totalStudents ”從 0 開始,因此您的 while 循環條件應僅更改為“<”,同時使用“ studentNumber ”變量來計算平均值。

while(totalStudents < studentNumber)
{
    Console.WriteLine("enter your student score");
    string studentScore = Console.ReadLine();
    if (int.TryParse(studentScore, out number))
    {
        totalScores += number;
        totalStudents += 1;
    }
    else
    {
        Console.WriteLine("you did not enter a number");
    }
    
}
int avarage = totalScores / studentNumber;

簡短的回答

簡短的回答是您應該使用while(totalStudents < studentNumber) 但讓我們探討一下原因。

長答案

我希望你專注於這個時間點:

while(totalStudents <= studentNumber)
{
    // <------ HERE

    Console.WriteLine("enter your student score");
    string studentScore = Console.ReadLine();
    
    // rest of code omitted      
}

換句話說,這是您決定注冊另一個學生之后,但您注冊該學生之前。

假設總共有 3 名學生,請告訴我每個循環中totalStudents的值是多少。 請在繼續之前嘗試解決它,這是作為初學者理解的基本技能。

第一個循環: totalStudents = 0
第二個循環: totalStudents = 1
第三個循環: totalStudents = 2
第四個循環: totalStudents = 3

總結:當開始循環 X 時, totalStudents等於 X-1

注意:我在列表中添加了第四個循環,因為您當前的代碼額外運行了一個循環,我想向您展示導致第四個循環的原因。

現在讓我們重復這個練習,但關注另一個時間點:

while(totalStudents <= studentNumber)
{
    Console.WriteLine("enter your student score");
    string studentScore = Console.ReadLine();
    
    // if block omitted      

    // <------ HERE
}

換句話說,這是您決定注冊另一個學生之后,並且您注冊該學生之后。 對於每個循環(假設 3 個學生),告訴我totalStudents的值。

第一個循環: totalStudents = 1
第二個循環: totalStudents = 2
第三個循環: totalStudents = 3
第四個循環: totalStudents = 4

總結:當完成循環 X 時, totalStudents等於 X

在第三個循環結束時, while將檢查是否需要啟動另一個循環。 要檢查這一點,它會檢查是否totalStudents <= studentNumber

在我的示例中,我們確定studentNumber為 3。但是在第三個循環執行后, totalStudents的值是多少? (提示:查看我列出的上述值)。

由於您現在知道totalStudentsstudentNumber的值,因此您可以看到totalStudents <= studentNumber將評估為true ,因此將啟動第四個循環。

但你不想要這個。 您希望它在studentNumber循環數之后停止。
另一種說法是,僅當您當前注冊的學生數量少於您希望注冊的學生總數時,才應啟動另一個循環。 當它們相等時,您不想開始另一個循環。

因此,正確的while評估應該使用小於運算符: totalStudents < studentNumber

名字里有什么?

作為一個很好的提示,請嘗試為變量使用更好的描述性名稱。 我建議以下重命名:

  • totalStudents => amount_of_students_you_have_entered_so_far
  • studentNumber => amount_of_students_you_will_enter_in_total

這些名字很長,但我讓它們非常具有描述性且易於理解,因為您是初學者。 現在讓我們再次使用新名稱重新訪問您的代碼(包含錯誤):

while(amount_of_students_you_have_entered_so_far <= amount_of_students_you_will_enter_in_total)
{        
    // rest of code omitted      
}

現在更清楚地看到您的評估存在缺陷。

我承認這些名字太長了。 但是,如果您難以解析代碼的含義,最好是過度描述,而不是讓它更難猜測。 經驗會隨着時間的推移而出現,然后您可以減少變量名稱的長度。

就我個人而言,我會使用studentCounterstudentTotal來表示一個是向上計數的值,另一個是這個計數值應該達到的最大限制。

我懷疑您無法發現該錯誤的部分原因是因為您使用“總數”一詞來指代輸入的學生人數totalStudents ),而您使用“數字”來指代您的學生總數將輸入 ( studentNumber )。 你換了個詞。

也許是因為在 while 循環中你放了小於或等於 5,所以它會在學生總數中增加 1 直到達到 5,並且它會在第五個中增加 1,使其成為 6。就是這樣,如果你只是改變它應該可以正常工作<= 只是 <

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM