簡體   English   中英

為什么在Visual Studio 2012中運行此保險計算器時不顯示輸出? (它調試完美)

[英]Why won't this insurance calculator display output when it's run in Visual Studio 2012? (It debugs perfectly)

如果我是業余愛好者,請原諒我,只做了幾周。 我必須構建一個應用程序,該應用程序可以根據年齡和所選擇的保險計划來計算每月的每月保險費和總保費。

我在calcButton_click事件之前做了一些方法,包括:

i)ageCatTest,以確定客戶所屬的年齡類別。

ii)自付額選​​擇確定他們將支付的自付額(0、75或100)。 我認為將所有3個放入列表框比嘗試編寫代碼包含這3個選項之外的答案要容易得多。

iii)計划選擇確定已選擇哪個計划(A或B)。 我還使用了一個列表框來嘗試包含可能答案的領域。

我在二維數組中設置了所有可能的月度保費,其中row [0]對應於計划A,row [1]對應於計划B。列[0,1,2,3,4]對應於年齡類別(AGECAT)。 然后,我嘗試進行設置,以便使用ageCat方法和planChoice方法的結果作為索引鍵從數組中選擇值。

最后,一旦確定了保費,便將其乘以潛在折扣(如果用戶在列表框中選擇75或100作為免賠額,則不是0)。

激活calcButton_click時,所有這些結果應該顯示在幾個文本框中,但實際情況並非如此。 當我點擊計算時,它不會凍結或滯后,什么也不會發生。

Visual Studio找不到任何錯誤,就編譯器而言,沒有任何錯誤。 但是,由於什么也沒有發生,所以我假設我構造方法或click_event的方式有問題,但我找不到任何東西。 如果有人發現任何明顯的錯誤,將不勝感激。

謝謝!

namespace InsuranceApp
{
public partial class insuranceCalculator : Form
{
    public insuranceCalculator()
    {
        InitializeComponent();
    }

    private bool planChoice(string plan)
    {
        bool planType = false;
        if (plan.Equals("B"))
        {
            planType = true;
            return planType;
        }
        return planType;
    }

    private decimal deductibleChoice(string deductibleSelect)
    {
        decimal discount;
        discount = 1;

        switch (deductibleSelect)
        {
            case "75":
                discount = 0.95m;
                return discount;

            case "100":
                discount = 0.92m;
                return discount;
        }

        return discount;
    }




    private int ageCatTest(int age)
    {
        int ageCat = 0;

        if (age >= 36 && age <= 45)
        {
            ageCat = 1;
            return ageCat;
        }
        else if (age >= 46 && age <= 55)
        {
            ageCat = 2;
            return ageCat;
        }
        else if (age >= 56 && age <= 65)
        {
            ageCat = 3;
            return ageCat;
        }
        else if (age >= 66 && age <= 75)
        {
            ageCat = 4;
            return ageCat;
        }

        return ageCat;
    }

    private void calcButton_Click(object sender, EventArgs e)
    {
        int[,] plans = {{80, 90, 110, 140, 170},
                   {100, 110, 125, 170, 210}};
        int ageCat;
        int planType = 0;
        int age;
        decimal discount;
        string deductible;
        decimal coverage = 100000;
        decimal monthlyPremium;
        int months;
        string plan;
        decimal totalPremium;

        if (int.TryParse((ageTextBox.Text), out age))
        {
            if (age < 18 || age > 75)
            {
                MessageBox.Show("Sorry, you are ineligible for travel insurance.");
            }
            else
            {
                if (int.TryParse((monthsTextBox.Text), out months))
                {
                    ageCat = ageCatTest(age);

                    plan = planListBox.SelectedItem.ToString();

                    if (planChoice(plan) == true)
                    {
                        planType = 1;
                        coverage = 150000;
                    }

                    coverageTextBox.Text = coverage.ToString("n2");

                    deductible = deducSelBox.SelectedItem.ToString();
                    discount = deductibleChoice(deductible);

                    monthlyPremium = plans[planType, ageCat];
                    monthlyPremium = monthlyPremium * discount;
                    premiumTextBox.Text = monthlyPremium.ToString("n2");


                    totalPremium = monthlyPremium * months;
                    totalPremTextBox.Text = totalPremium.ToString("n2");
                }
            }
        }
    }



    private void exitButton_Click(object sender, EventArgs e)
    {
        this.Close();
    }
}

}

計算機錯誤的一般分類是:編譯時錯誤,運行時錯誤和邏輯錯誤。

我會同意@Jay和@Steve。 這里的問題更多是隱藏的邏輯錯誤,而不是編譯時錯誤。 解析文本框后缺少“ else”語句意味着“無聲失敗”。 而是在使用try ... catch塊進行解析的適當else語句中引發錯誤,並相應地處理捕獲的異常。

對於“其他”情況,一個簡單的解決方案如下所示(偽代碼):

if (try.parse (text box) == success) {
  execute program
}
else {
  message box show (relevant error message)
}

對於“ try..catch”情況,一種簡單的解決方案是(偽代碼):

try {
  parse (text box)
  execute program 
}
catch (Parsing Exception as e) {
  message box show (relevant error message)
}

暫無
暫無

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

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