study

[C#] Chapter 11. Sum of Three Random Numbers (test)

yz 2022. 6. 9. 11:18

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 Chap11_Test04_Review : Form
    {
        int[] iArray = new int[3];
        int iCount = 0;

        public Chap11_Test04_Review()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 난수를 생성
            Random rnd = new Random();
            int iValue = rnd.Next(0, 100);
            if (iCount == 0)      txtRan1.Text = Convert.ToString(iValue);
            else if (iCount == 1) txtRan2.Text = Convert.ToString(iValue);
            else if (iCount == 2) txtRan3.Text = Convert.ToString(iValue);
            iArray[iCount] = iValue;
            ++iCount;
            if (iCount == 3)
            {
                iCount = 0;
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            // 결과 버튼 클릭
            Array.Sort(iArray); // int[] 배열에 있는 값을 정렬 0 : 최소값, 2 : 최대값
            int iResult = 0; // 합을 누적시킬 결과 변수
            for (int i = 0; i < iArray.Length; i++)
            {
                iResult += iArray[i];
            }
            
            if (iResult < 100)
            {
                MessageBox.Show($"3 수의 합이 100 미만, 최소값 {iArray[0]}, 최대값 {iArray[2]}의 곱은 {iArray[0] * iArray[2]}");
            }
            else if (iResult >= 100 && iResult < 200)
            {
                MessageBox.Show($"3 수의 합이 100 이상 200 미만, 최소값 {iArray[0]}, 최대값 {iArray[2]}의 합은 {iArray[0] + iArray[2]}");
            }
            else if (iResult >= 200)
            {
                MessageBox.Show($"3 수의 합이 200이 넘습니다.");
            }
        }
    }
}

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)
        {
            Random Ran = new Random();
            textBox1.Text = Convert.ToString(Ran.Next(0, 100));
            textBox2.Text = Convert.ToString(Ran.Next(0, 100));
            textBox3.Text = Convert.ToString(Ran.Next(0, 100));
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int i = Convert.ToInt32(textBox1.Text);
            int j = Convert.ToInt32(textBox2.Text);
            int k = Convert.ToInt32(textBox3.Text);
            int Sum = i + j + k;
            int max = 0;
            int min = 0;
            if (i >= j)
            {
                max = i;
                min = j;
                if (j >= k) // i >= j >= k
                {
                    //max = i;
                    min = k;
                }
                else // k > j
                {
                    if (k >= i) // k >= i >= j
                    {
                        max = k;
                        //min = j;
                    }
                    //else // i > k > j
                    //{
                    //    max = i;
                    //    min = j;
                    //}
                }
            }
            else // j > i
            {
                max = j;
                min = i;
                if (i >= k) // j > i >= k
                {
                    //max = j;
                    min = k;
                }
                else // k > i
                {
                    if (k >= j) // k >= j > i
                    {
                        max = k;
                        //min = i;
                    }
                    //else // j > k > i
                    //{
                    //    max = j;
                    //    min = i;
                    //}
                }
            }

            if (Sum < 100)
            {
                MessageBox.Show($"{min} * {max} = {min * max}");
            }
            else if (Sum >= 100 && Sum < 200)
            {
                MessageBox.Show($"{min} + {max} = {min + max}");
            }
            else // Sum >= 200
            {
                MessageBox.Show("세 수의 합이 200 이 넘습니다.");
            }
        }
    }
}
LIST