การบ้านครั้งที่6

โปรแกรมที่5.3
using System;

namespace Calculatelnterst
{

    class Program
    {
        static void Main()
        {
            Console.Write("Enter principal: ");
            string sPrincipal = Console.ReadLine();
            decimal mPrincipal = Convert.ToDecimal(sPrincipal);
            if (mPrincipal < 0)
            {
                Console.WriteLine("Principal cannot be negative");
                mPrincipal = 0;
            }
            Console.Write("Enter interest");
            string slnterest = Console.ReadLine();
            decimal mlnterest = Convert.ToDecimal(slnterest);
            if (mPrincipal < 0)
            {
                Console.WriteLine("Principal cannot be negative");
                mPrincipal = 0;
            }
            decimal mlnterestPaid;
            mlnterestPaid = mPrincipal * (mlnterest / 100);
            decimal mTotal = mPrincipal + mlnterestPaid;
            Console.WriteLine();
            Console.WriteLine("Principal = " + mPrincipal);
            Console.WriteLine("Interest = " + mlnterest + "%");
            Console.WriteLine();
            Console.WriteLine("lnterest paid = " + mlnterestPaid);
            Console.WriteLine("Total = " + mTotal);
            Console.WriteLine("Press Enter to terminate...");
            Console.Read();
        }
    }
}




 โปรแกรมที่ 5.13

using System;

namespace CalculatelnterestTableMoreForgiving
{
    class Program
    {
        static void Main(string[] args)
        {
            int nMaximumlnterest = 50; //กำหนดค่าดอกเบี้ยสูงสุด
            decimal mPrincipal;
            while (true) //วนลูปปรับเงินต้นที่มีค่าเป็นบวก
            {
                Console.WriteLine("Enter principal : ");
                string sPrincipal = Console.ReadLine();
                mPrincipal = Convert.ToDecimal(sPrincipal);
                if (mPrincipal >= 0)
                {
                    break; //ถ้าหากมีค่าเป็นบวกออกมาจากวนลูป
                }
                Console.WriteLine("Principal cannot be negatine");
                Console.WriteLine("Try agian");
                Console.WriteLine();
            }
            decimal mlnterest;
            while (true) //วนลูปปรับอัตราดอกเบี้ยที่มีค่าเป็นบวกและไม่เกินค่าสูงสุด
            {
                Console.Write("Enter interest");
                string slnterest = Console.ReadLine();
                mlnterest = Convert.ToDecimal(slnterest);
                if (mlnterest > 0 && mlnterest <= nMaximumlnterest)
                {
                    break; //ถ้าหากมีค่าเป็นบวกและน้อยกว่าค่าสูงสุดจะออกจากลูป
                }
                Console.WriteLine("lnterest cannot be negative" +
                "or greater than " + nMaximumlnterest);
                Console.WriteLine("Try again");
                Console.WriteLine();
            }
            Console.Write("Enter number of year: ");
            string sDuration = Console.ReadLine();
            int nDuration = Convert.ToInt32(sDuration);
            Console.WriteLine();
            Console.WriteLine("Principal = " + mPrincipal);
            Console.WriteLine("lnterest = " + mlnterest + "%");
            Console.WriteLine("Duration = " + nDuration + "year");
            Console.WriteLine();
            int nYear = 1;
            while (nYear <= nDuration)
            {
                decimal mlnterestPaid;
                mlnterestPaid = mPrincipal * (mlnterest / 100);
                mPrincipal = mPrincipal + mlnterestPaid;
                mPrincipal = decimal.Round(mPrincipal, 2);
                Console.WriteLine(nYear + " - " + mPrincipal);
                nYear = nYear + 1;
            }
            Console.WriteLine("Prees Enter to terminate...");
            Console.Read();
        }
    }
}

โปรแกรมที่7.7

using System;
namespace studentname
{
    class student
    {
        private int idNumber;
        private string lastName;
        private double gradePointAversge;
        public int getld()
        {
            return idNumber;
        }
        public string getName()
        {
            return lastName;
        }
        public double getGPA()
        {
            return gradePointAversge;
        }
        public void setLd(int id)
        {
            idNumber = id;
        }
        public void setName(string name)
        {
            lastName = name;
        }
        public void setGPA(double gpa)
        {
            gradePointAversge = gpa;
        }
        class Program
        {
            static void Main()
            {
                student st = new student();
                st.setLd(9353);
                st.setName("Teerawat");
                st.setGPA(3.98);
                Console.WriteLine("Student name {0} has ID # {1} and gpa of{2}",
                    st.getName(), st.getld(), st.getGPA());
                Console.ReadLine();
            }
        }
    }
}

โปรแกรมที่8.6


using System;
namespace UserPredefineMethods
{
    static class Program
    {
        static void Main()
        {
            double[] waterDepht = { 45, 19, 2, 16.8, 190, 0.8, 510, 6, 18 };
            string outputMsg = "";
            string caption = "System.Array Methods IIIustrated";
            double[] w = new double[20];
            outputMsg += "waterDepth Array\n\n";
            foreach (double wVal in waterDepht)
                outputMsg += wVal + "\n";
            MessageBox.Show(outputMsg, caption);
            Array.Copy(waterDepht, 2, w, 0, 5);
            Array.Sort(w);
            outputMsg = "Array w Sorted\n\n";
            foreach (double wVal in w)
            {
                if (wVal > 0)
                    outputMsg += wVal + "\n";
            }
            MessageBox.Show(outputMsg, caption);
            Array.Reverse(w);
            outputMsg = "Array w Reversed\n\n";
            foreach (double wVal in w)
            {
                if (wVal > 0)
                    outputMsg += wVal + "\n";
            }
            MessageBox.Show(outputMsg, caption);
        }
    }
}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น