我仍然在學習與Windows窗體C#
,我想要一些幫助我的if語句。需要幫助搞清楚我在做什麼錯我的if語句
變量是固定的,所以除了他們只會點擊一個按鈕之外,沒有用戶輸入。我的問題開始於if語句的開頭。
VS(Visual Studios)不斷地說,half
和calc
中的條件,以及hours
用於轉換爲字符串都是未分配的局部變量。
然後在數學方程的else語句中,VS一直說「小時」不能從double
轉換爲hours
。
我不知道這是什麼意思,因爲我已經宣佈他們。然後它說thirtheenpercent
不能在範圍內聲明,因爲它已經被聲明。我不知道爲什麼會這樣說,因爲這個雙打不應該改變。
有人可以幫我解釋爲什麼我得到這些錯誤嗎? 任何幫助將不勝感激。
decimal onecup = 130;
decimal thirtheenpercent;
decimal hours=0;
decimal half=130/2;
decimal calc=0;
while (onecup>half)
{
thirtheenpercent = (onecup * 13)/100;
calc =onecup-thirtheenpercent;
onecup=calc;
hours++;
} mtbHalf.Text = Convert.ToString(hours)+" Hours.";
while (!(hours==24))
{
thirtheenpercent = (onecup * 13)/100;
calc = onecup - thirtheenpercent;
onecup = calc;
hours++;
} mtbOnecup24.Text = Convert.ToString(Math.Round(calc,2)) + " mg.";
//While i was running this program, i ran into some conflictions with my already declared variables for my last while loop so i created some new ones.//
decimal thirtheenpercent2;
decimal calc2 = 0;
decimal onecup2 = 130;
int hours2 = 0;
while (!(hours2 == 24))
{
thirtheenpercent2 = (onecup2 * 13)/100;
calc2 = onecup2 - thirtheenpercent2;
onecup2 = calc2;
hours2++;
onecup2 = onecup2 + 130;
} mtbQuantity.Text = Convert.ToString(Math.Round(onecup2, 2)) + " mg.";
}
好吧,'hours' _is_ unassigned。就'half'和'calc'而言,也許你只是誤解了錯誤信息。 – 2015-04-04 02:00:39
你不能寫'hours * thirtheenpercent = calc'。任務必須涉及左側的一些存儲位置;你不能給表達式分配一些值。 – 2015-04-04 02:01:56
另請注意,'double'類型是近似的,所以你可能永遠不會碰到你的'half == calc'條件。由於雙打的行爲方式,你開始「half」爲65,「calc」最終可能爲65.0000000001或64.99999998,參見[這裏] point_format) – 2015-04-04 02:04:32