study

[C#] Chapter 11. Fees for Rides (test)

yz 2022. 6. 9. 16:45

 

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 MyFirstCSharp_01
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private void btnRanMake_Click(object sender, EventArgs e)
        {
            Random ran = new Random();
            GetPrice(ran.Next(100, 3000), ran.Next(1, 20), ran.Next(10000, 500000));
        }

        private void GetPrice(int iPrice, int iCount, int iCash)
        {
            // 내가 지불해야 할 금액
            int iResult = 0; // 지불해야 할 누적 금액
            for (int i = 1; i <= iCount; i++)
            {
                iResult += iPrice * i;
            }
            // 내가 지불하고 남는 금액
            int iRemain = 0;
            iRemain = iCash - iResult;

            string sMessage = "남습니다.";
            if (iRemain < 0) sMessage = "모자랍니다.";

            MessageBox.Show($"{iCash} 이 있을 때 이용요금 {iPrice} 인 놀이기구 {iCount} 번 타면 {iRemain} 이 {sMessage}");
        }
    }
}

my code

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 MyFirstCSharp_01
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int TotalPrice = 0;
            Random Ran = new Random();
            int Cash = Ran.Next(10000, 500000);
            int LeftCash = Cash;
            int Number = Ran.Next(1, 20);
            int Price = Ran.Next(1000, 3000);
            for (int i = 1; i <= Number; i++)
            {
                TotalPrice += Price * i;
            }
            LeftCash -= TotalPrice;
            if (LeftCash >= 0)
            {
                MessageBox.Show($"{Cash} 원이 있을 때 이용요금 {Price} 원인 놀이기구를 {Number} 번 타면 {LeftCash} 원이 남습니다.");
            }
            else
            {
                MessageBox.Show($"{Cash} 원이 있을 때 이용요금 {Price} 원인 놀이기구를 {Number} 번 타면 {-LeftCash} 원이 모자랍니다.");
            }
        }
    }
}
LIST