コード例 #1
0
        void CalcButt_Click(object sender, EventArgs e)
        {
            EditText nIn = FindViewById <EditText>(Resource.Id.editN);
            EditText pIn = FindViewById <EditText>(Resource.Id.editP);
            EditText rIn = FindViewById <EditText>(Resource.Id.editR);
            EditText aIn = FindViewById <EditText>(Resource.Id.editA);

            string nTxt = nIn.Text;
            string pTxt = pIn.Text;
            string rTxt = rIn.Text;
            string aTxt = aIn.Text;

            int    n = 0;
            double p = 0;
            int    r = 0;
            double a = 0;

            bool nInS;
            bool pInS;
            bool rInS;
            bool aInS;

            nInS = Int32.TryParse(nTxt, out n);
            pInS = double.TryParse(pTxt, out p);
            rInS = Int32.TryParse(rTxt, out r);
            aInS = double.TryParse(aTxt, out a);


            Toast nBad = Toast.MakeText(this, "n = " + nTxt + " is not valid", ToastLength.Short);
            Toast pBad = Toast.MakeText(this, "p = " + pTxt + " is not valid", ToastLength.Short);
            Toast rBad = Toast.MakeText(this, "r = " + rTxt + " is not valid", ToastLength.Short);
            Toast aBad = Toast.MakeText(this, "a = " + aTxt + " is not valid", ToastLength.Short);

            bool inError = false; // __________ Displaying Error Messages: __________

            if (!nInS)            // Error messages are only displayed if no previous errors were shown, so errors can be dealt with individually.
            {
                if (!inError)
                {
                    nBad.Show(); // Show error message
                    inError = true;
                }
            }
            if (!pInS || p > 1)
            {
                if (!inError)
                {
                    pBad.Show(); // Show error message
                    inError = true;
                }
            }
            if (!rInS || r > n)
            {
                if (!inError)
                {
                    rBad.Show(); // Show error message
                    inError = true;
                }
            }
            if (!aInS)
            {
                if (!inError)
                {
                    aBad.Show(); // Show error message
                    inError = true;
                }
            } // __________ End of Error Messages __________


            if (!inError)                                                              // Calculations only carried out if no parsing/maths errors were found.
            {
                TextView p_XetX     = FindViewById <TextView>(Resource.Id.p_XetX);     // First textview
                TextView p_XltORetX = FindViewById <TextView>(Resource.Id.p_XltORetX); // Second textview

                // Get values from Maths class and convert to string
                string p_XetX_string     = "P(X = x)= " + ((Maths.BinomialProb_XetR(n, p, r)).ToString("0.0000"));
                string p_XltORetX_string = "P(X ≤ x)= " + (Maths.BinomialProb_XltORetR(n, p, r)).ToString("0.0000");
                // Assign string values to label text
                p_XetX.Text     = p_XetX_string;
                p_XltORetX.Text = p_XltORetX_string;

                // Labels centred
            }
        }