최대최소값출력하기

신동원·2021년 9월 23일
0

c#

목록 보기
1/2
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 max_mini
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    int[] a = new int[1000];
    
    int iarraycount = 0;

    private void button1_Click(object sender, EventArgs e)
    {
        a[iarraycount] = Convert.ToInt32(textBox1.Text);
        iarraycount++;
        if (textBox2.Text == "")
        {
            textBox2.Text = textBox2.Text + textBox1.Text;
        }
        else
        {
            textBox2.Text = textBox2.Text + "," + textBox1.Text;
        }
        textBox1.Clear();
        textBox1.Focus();


    }

    private void button2_Click(object sender, EventArgs e)
    {
        int max = int.MinValue;
        for(int i = 0; i < iarraycount; i++)
        {
            if (max < a[i])
                max = a[i];
        }
        MessageBox.Show("최대값은 " + max + "이다.");
    }

    private void button3_Click(object sender, EventArgs e)
    {
        int min = int.MaxValue;
        for (int i = 0; i < iarraycount; i++)
        {
            if (min > a[i])
                min = a[i];
        }
        MessageBox.Show("최소값은 " + min + "이다.");
    }
}
}


UI의 사진이다. 위 텍스트박스에 숫자를 입력하고 입력을 누르면 아래 텍스트박스에 숫자가 입력된다.


a[iarraycount] = Convert.ToInt32(textBox1.Text);
iarraycount++;
if (textBox2.Text == "")
{
textBox2.Text = textBox2.Text + textBox1.Text;
}
else
{
textBox2.Text = textBox2.Text + "," + textBox1.Text;
}
textBox1.Clear();
textBox1.Focus();
입력버튼을 눌렀을 때의 동작이다. 배열[0]에 텍스트박스1의 문자를 인트형으로 변환하여 넣어준다. 그이후 배열은 +1해준다.
만약 텍스트박스2가 비어있다면 텍스트박스2에 텍스트박스2+텍스트박스1의 문자를 넣어주고
그게아니라면 텍스트박스2 + , + 텍스트박스1의 문자를 넣어준다.
첫번째 문자는 쉼표를 생략하기위해 if문을 사용하였다.


int max = int.MinValue;
for(int i = 0; i < iarraycount; i++)
{
if (max < a[i])
max = a[i];
}
MessageBox.Show("최대값은 " + max + "이다.");
최대값과 최소값을 구하는 소스는 거의 동일하다. 해당버튼을 눌렀을 때 배열에 저장되어있는 값들을 순차대로 비교하여주고 가장 높거나 작은값을 메세지박스에 출력해주는 방식이다.


HTML Your browser does not support the video tag.
profile
오늘보다 내일 더 나은 사람이 되기 위해 노력하자

0개의 댓글