简体   繁体   中英

C# Calculation not correct

When I run the code, it doesn't calculate and it doesn't show the calculations. Wondering if my variables or something else is wrong or maybe in the wrong place.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Question_3_Retail_Price_Calculator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            decimal costPrice = 0;
            decimal markUpRateA = 0.19m;
            decimal markUpRateB = 0.14m;
            decimal markUpRateC = 0.12m;
            decimal markUpRate = 0m;
            decimal markUpTotal = 0;
            decimal totalCost = 0;

            // Add the items to ListBox 1
            listBox1.Items.Add("Markup Rate =");
            listBox1.Items.Add("Markup Total =");
            listBox1.Items.Add("Total Cost =");

            try
            {
                // Read the cost price from the user
                costPrice = decimal.Parse(textBox1.Text);

            }
           catch (System.Exception excep )
             {
                 MessageBox.Show(excep.Message);
             }

                if (costPrice <= 50)
                {
                    // Calculate the value of  the product 
                    costPrice = (costPrice / 100) * markUpRateA;  
                }
                else if (costPrice >= 50 && costPrice < 100)
                {
                    //  Calculate the value of the product
                    costPrice = (costPrice / 100) * markUpRateB;
                }
                else if (costPrice < 100)
                {
                    // Calculate the value of the product
                    costPrice = (costPrice / 100) * markUpRateC;
                }
                else if (markUpRate == markUpTotal)
                {
                    // Calculate the total monetary amount
                    markUpTotal = costPrice * markUpRate;
                }
                else
                {
                    // Calculate the Total Cost  of the product
                    totalCost = costPrice + markUpTotal;
                }
                // Output the total Cost
                MessageBox.Show("Total Cost is: €"  + totalCost);
            }
    }
}

Need some help figuring this out! Thanks in advance!

You're setting your total cost in the last else condition which will be executed only if all the other if-else conditions are not executed. That is why this code isn't working as you expect.

Even if you enter a value >100, your result is never assigned to totalCost. Because execution enters this part of your code

         else if (markUpRate == markUpTotal)
        {
            // Calculate the total monetary amount
            markUpTotal = costPrice * markUpRate;
        }

and then jumps directly to the Message.Show(..) line.

There is a lot of crossover in you if else statements. If costPrice is 50, do you want to run rate A, rate B, or rate C (I think you have put your > sign the wrong way with markup rate C).

Also, why is it in a try loop? You are basically saying: "If there are no problems, assign costPrice to whatever the user entered, then skip the rest. And if there is a problem, do the rest but the costPrice will be the default value I assigned it to at the start (0)."

Basically, all the rest of the stuff should be in the try loop, not the catch loop.

Oh and read Xaero's answer as well, I have to wite this here though because i'm not allowed not comment yet :(.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM