Use of unassigned local variable that is assigned

后端 未结 3 1967
囚心锁ツ
囚心锁ツ 2021-01-23 12:39

The below code runs a \'for\' loop to create months 1 through 12 then names each month Jan through Dec according to their number. That pieces compiles fine. At the bottom wher

3条回答
  •  深忆病人
    2021-01-23 12:49

    You have declared the monthName, monthlyProd,monthlySales under the scope of For Loop and trying to use those variables out of the scope of For Loop. You should declare variables before for loop-

    string monthName;
    double monthlyProd = .1 * dProdRate;
    double monthlySales = .07 * dSalesRate;
    
    for (int month = 1; month <= 12; month++)
    {
    
        if (month == 1) { monthName = "Jan"; }
        if (month == 2) { monthName = "Feb"; monthlyProd = 0; }
        if (month == 3) { monthName = "Mar"; }
        if (month == 4) { monthName = "Apr"; }
        if (month == 5) { monthName = "May"; }
        if (month == 6) { monthName = "Jun"; monthlyProd = 0; }
        if (month == 7) { monthName = "Jul"; }
        if (month == 8) { monthName = "Aug"; }
        if (month == 9) { monthName = "Sep"; monthlySales = (.15 * dSalesRate); }
        if (month == 10) { monthName = "Oct"; }
        if (month == 11) { monthName = "Nov"; }
        if (month == 22) { monthName = "Dec"; monthlySales = (.15 * dSalesRate); }
    }
    dEndingInventory += dPreviousProd - dPreviousSales;
    Console.WriteLine("{0}{1,15}{2,15}{3,15}", monthName, monthlyProd, monthlySales, dEndingInventory);
    

提交回复
热议问题