2010-07-18 21 views
0

即時進行練習,我必須製作停車場應用程序。 目標是:使用numericUpDown控件創建表單,以選擇車輛停放的時間量。添加選項按鈕以顯示車輛是汽車還是卡車。清楚地標出每個盒子和按鈕。添加標籤或文本框以顯示付款金額。我不得不設置一些限制。汽車24小時不應超過38美元,24小時不超過44.50美元。如何檢查文本框中的數字是否大於預定義值

我發現的唯一問題是我無法設置這些限制。它給出了一個錯誤:使用未賦值的局部變量'result'。

查看評論///的代碼。

任何幫助非常感謝。這不是一個功課項目,我只是自己學習C#並做一些練習。

下面的代碼:

 private void calculate_Click(object sender, EventArgs e) 
    { 
     double carhours = 3; 
     double truckhours = 3.50; 
     double carFirsthour = 5; 
     double truckFirsthour = 6; 
     int hrs = (int)numericUpDown.Value; 
     double result; 
     int maxCarFee = 38; 


     if (numericUpDown.Value < 1) 
     { 
      MessageBox.Show("NO WAY"); 
      return; 
     } 

     if (carButton.Checked == true && (numericUpDown.Value == 1)) 
     { 
      result = (hrs * carFirsthour); 
      fee.Text = "$" + result; 
     } 

     if (carButton.Checked == true && (numericUpDown.Value > 1)) 
     { 
      result = ((hrs - 1) * carhours + carFirsthour); 
      fee.Text = "$" + result; 
     } 

     if (truckButton.Checked == true && (numericUpDown.Value == 1)) 
     { 
      result = (hrs * truckFirsthour); 
      fee.Text = "$" + result; 
     } 

     if (truckButton.Checked == true && (numericUpDown.Value > 1)) 
     { 
      result = ((hrs - 1) * truckhours + truckFirsthour); 
      fee.Text = "$" + result; 
     } 

     /// limits 

     ///if (carButton.Checked == true && (numericUpDown.Value < 24) && (result > maxCarFee)) 
     ///{ 
     /// fee.Text = "$" + maxCarFee; 
     ///} 
    } 
+0

如果你將添加你得到的錯誤,幫助會容易得多。 – 2010-07-18 13:50:37

+0

對不起,只是。 – Manolis 2010-07-18 13:53:58

回答

1

嘗試這樣:

private void calculate_Click(object sender, EventArgs e) { 
    double carhours = 3.0; 
    double truckhours = 3.5; 
    double carFirsthour = 5.0; 
    double truckFirsthour = 6.0; 
    double maxCarFee = 38.0; 
    double maxTruckFee = 44.5; 

    int hrs = (int)numericUpDown.Value; 
    double result; 

    if (hrs < 1) { 
     MessageBox.Show("NO WAY"); 
     return; 
    } 

    if (carButton.Checked) { 
     result = (hrs-1) * carhours + carFirsthour; 
     if (result > maxCarFee) { 
      result = maxCarFee; 
     } 
    } 
    else { 
     result = (hrs-1) * truckhours + truckFirsthour; 
     if (result > maxTruckFee) { 
      result = maxTruckFee; 
     } 
    } 

    fee.Text = "$" + result; 
} 

但是,你真的不說清楚會發生什麼,如果一個汽車/卡車停留超過24小時(或48小時以上)。你可能最終得到的東西更一致的結果是這樣的:

private void calculate_Click(object sender, EventArgs e) { 
    double hourlyFee = 3.0; 
    double firstHourFee = 5.0; 
    double max24hrFee = 38.0; 

    if (!carButton.Checked) { 
     hourlyFee = 3.5; 
     firstHourFee = 6.0; 
     max24hrFee = 44.5; 
    } 

    int hrs = (int)numericUpDown.Value; 
    if (hrs < 1) { 
     MessageBox.Show("NO WAY"); 
     return; 
    } 

    double result = 0.0; 
    if (hrs >= 24) { 
     result = (hrs/24) * max24hrFee; 
     hrs = hrs % 24; 
    } 

    if (hrs > 0) { 
     result = result + firstHourFee + ((hrs-1) * hourlyFee); 
    } 

    fee.Text = "$" + result; 
} 
+0

好的清理,除了「== true」也可以刪除,如果它不損害代碼的可讀性。我慢慢打字,所以每個字符都是重要的:P – SirDemon 2010-07-18 13:57:48

1

需要numericUpDown.Value 轉換爲INT32

或監守你這樣做:

int hrs = (int)numericUpDown.Value; 

你可以用小時來代替的NumericUpDown 。if語句中的值

+0

是的,不知道爲什麼我錯過了!謝謝。 但問題是'結果'變量。 現在的代碼給出了一個錯誤:使用未分配的局部變量'結果' – Manolis 2010-07-18 13:52:19

0

您檢查if numericUpDown.Value小於24,我認爲你的意思是更大(>)?!

使用的未分配的局部變量「結果」

意味着你沒有初始化的變量。你必須這樣做

result = 0 

在你去if構造之前。

-1
int hrs = (int)numericUpDown.Value; 

您在小時在你的條件是否獲得價值,所以你可以使用這個。

if (carButton.Checked == true && (numericUpDown.Value == 1)) 

寫下如下。沒有必要的== true

if (carButton.Checked && (numericUpDown.Value == 1)) 
0

的問題是,沒有任何條件(if)語句執行這樣result從未賦值。在其聲明中指定result - double result = 0D; - 並且代碼將運行,以便您可以調試條件邏輯。

相關問題