有些数据在计算时丢失了

时间:2013-07-31 16:55:57

标签: c# winforms

更新:每当在类2中输入值时,显示的唯一结果是最后输入的输入。因此,例如:如果在class1行和class2行中输入数据,则仅显示class2行的计算。有人可以告诉我为什么会这样吗?

gpa Calculator

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;
using System.IO;

namespace Grade_Point_Average_Calculator
{
    public partial class GPA_Calculator : Form
    {
        SaveFileDialog saveFileDialog1;

        public GPA_Calculator()
        {
            InitializeComponent();
        }

        private void GPA_Calculator_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }


        //  Closes application after exitToolStripMenuItem_Click
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        // Displays information about the application version number and creator
        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Version 1.0., "About");
        }

        private void calculateBtn_Click(object sender, EventArgs e)
        {
            int maskbox1, maskbox2, maskbox3, maskbox4, maskbox5, maskbox6;

        CalculatorLogics oCalculatorLogics1 = new CalculatorLogics(this);
        oCalculatorLogics1.SelectedGrade = double.Parse(comboBox1.SelectedValue.ToString());
        if (!int.TryParse(maskedTextBox1.Text, out maskbox1))
        {
            maskbox1 = 0; // Assign zero is parse fails
        }
        else 
        {
            oCalculatorLogics1.CourseCredit = int.Parse(maskedTextBox1.Text);
        }
        oCalculatorLogics1.performGpaCalculations();
        answerLabel.Text = oCalculatorLogics1.CalcGrade.ToString();


        CalculatorLogics oCalculatorLogics2 = new CalculatorLogics(this);
        oCalculatorLogics2.SelectedGrade =            double.Parse(comboBox2.SelectedValue.ToString());
        if (!int.TryParse(maskedTextBox2.Text, out maskbox1))
        {
            maskbox2 = 0; // Assign zero is parse fails
        }
        else
        {
            oCalculatorLogics2.CourseCredit = int.Parse(maskedTextBox2.Text);
        }
        oCalculatorLogics2.performGpaCalculations();
        answerLabel.Text = oCalculatorLogics2.CalcGrade.ToString();

        }
    }
}


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

namespace Grade_Point_Average_Calculator
{
    class CalculatorLogics
    {

        private double grade;
        private double gradeValue;
        private double calculateGrades;
        private int credits;
        private GPA_Calculator _GPA_Calculator;

        // Pass form to CalculatorLogics constructor
        public CalculatorLogics(GPA_Calculator theGPA_Calculator)
        {
            _GPA_Calculator = theGPA_Calculator;
        }

        public double SelectedGrade 
        {
            get { return grade; }
            set { grade = value; }
        }

        public int CourseCredit 
        {
            get { return credits; }
            set { credits = value; }
        }

        public double CalcGrade 
        {
            get 
            {
            return calculateGrades; 
            }
        }

        public void performGpaCalculations() 
        {
            gradeVal = grade * credits;
            totalCredits += credits; // Add the amount of credits
            totalGradeValue += gradeVal;

            calculateGrades = totalGradeValue/totalCredits; // Calculates gpa
            return calculateGrades;
        }
    }
}

2 个答案:

答案 0 :(得分:2)

正如代码所示,您的问题是您没有在performGpaCalculations()课程中调用CalculatorLogics方法,这导致calculateGrades返回的CalcGrade值property是double(0.0)的默认值。

您需要在代码中调用performGpaCalculations()方法,如下所示:

private void calculateBtn_Click(object sender, EventArgs e)
{
    CalculatorLogics oCalculatorLogics = new CalculatorLogics();
    oCalculatorLogics.performGpaCalculations();
    answerLabel.Text = oCalculatorLogics.CalcGrade.ToString();
}

这将允许CalcGrade类确定calculateGrades

的值

更新:

但是你真正的问题是你正在实例化一个表单类GPA_Calculator的新实例,它会吹掉用户选择的值。

相反,您需要将GPA_Calculator表单类的实例传递给CalculatorLogics类的构造函数,如下所示:

public CalculatorLogics(GPA_Calculator theGPA_Calculator)
{

}

现在您的表单正在传递给构造函数,您需要在某处存储它以便CalculatorLogics类可以使用它,这里是对CalculatorLogics类的重写以支持此:

public class CalculatorLogics
{
    private double grade;
    private double gradeValue;
    private double calculateGrades;
    private int credits;
    private GPA_Calculator _GPA_Calculator;

    public CalculatorLogics(GPA_Calculator theGPA_Calculator)
    {
        _GPA_Calculator = theGPA_Calculator;
    }

    public double SelectedGrade 
    {
        get { return grade; }
        set { grade = value; }
    }

    public int CourseCredit 
    {
        get { return credits; }
        set { credits = value; }
    }

    public double CalcGrade 
    {
        get 
        {
        return calculateGrades; 
        }
    }

    public void performGpaCalculations() 
    {
        const double grade_A = 4.00;
        const double grade_A_minus = 3.67;
        const double grade_B_plus = 3.33;
        const double grade_B = 3.00;
        const double grade_B_minus = 2.67;
        const double grade_C_plus = 2.33;
        const double grade_C = 2.00;
        const double grade_C_minus = 1.67;
        const double grade_D = 1.33;
        const double grade_F = 0.00;

        switch (_GPA_Calculator.comboBox1.SelectedItem.ToString()) 
        {
            case "A":
                gradeValue = grade_A;
                break;
            case "A-":
                gradeValue = grade_A_minus;
                break;
            case "B+":
                gradeValue = grade_B_plus;
                break;
            case "B":
                gradeValue = grade_B;
                break;
            case "B-":
                gradeValue = grade_B_minus;
                break;
            case "C+":
                gradeValue = grade_C_plus;
                break;
            case "C":
                gradeValue = grade_C;
                break;
            case "C-":
                gradeValue = grade_C_minus;
                break;
            case "D":
                gradeValue = grade_D;
                break;
           case "F":
                gradeValue = grade_F;
                break;
        }
        calculateGrades = gradeValue * credits;
    }
}

最后,当您实例化CalculatorLogics课程时,您需要传递对您所在表格的引用,如下所示:

CalculatorLogics oCalculatorLogics = new CalculatorLogics(this);

注意:this是指向类本身的指针,在本例中为GPA_Calculator形式。

答案 1 :(得分:1)

要获取组合框的选定值,仅创建表单类的新实例没有帮助。

相反,您可以将当前表单传递给方法,如:

public void performGpaCalculations(GPA_Calculator form)

或更好地传递所选项目的列表:

public void performGpaCalculations(int[] selectedValues)

int []因为我建议你不要使用comboBox的selectedItem但是selectIndex并创建一个静态成绩数组

double[] grades = {4.00, 3.67, // ...

所以你不需要那个开关。

更新: 好的,上面的答案是根据我写的内容更新的,所以我的答案将不再有用......