study
[C#] Chapter 10. Loop - for (test)
yz
2022. 6. 2. 10:43
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 Chap10_Loop01_For_Test : Form
{
public Chap10_Loop01_For_Test()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// 2의 배수 누적 합 찾기
ShowMessage();
}
private void button2_Click(object sender, EventArgs e)
{
// 5의 배수 누적 합 찾기
ShowMessage(5);
}
private void button3_Click(object sender, EventArgs e)
{
// 10의 배수 누적 합 찾기
ShowMessage(10);
}
private void ShowMessage(int iValue =2)
{
// 변수 지정 (1부터 코딩의 흐름에 따라 그때그때 생성)
int iResult = 0; // 결과값 (숫자로 변경한 결과값)
string sStartValue = textBox1.Text;
string sEndValue = textBox2.Text;
// 벨리데이션 체크
int iResult1 = 0; // 시작 문자를 숫자로 변경할 int 변수
int iResult2 = 0; // 종료 문자를 숫자로 변경할 int 변수
bool bValueFlag; // 숫자 변경 결과
// 숫자여부 판단
bValueFlag = int.TryParse(sStartValue, out iResult1);
//if (!bValueFlag)
//{
// MessageBox.Show("숫자로 바뀔 수 없는 값을 입력하였습니다.");
// return;
//}
//bValueFlag = int.TryParse(sStartValue, out iResult2);
//if (!bValueFlag)
//{
// MessageBox.Show("숫자로 바뀔 수 없는 값을 입력하였습니다.");
// return;
//}
if (bValueFlag == true)
{
bValueFlag = int.TryParse(sEndValue, out iResult2);
}
if (!bValueFlag)
{
MessageBox.Show("숫자로 바뀔 수 없는 값을 입력하였습니다.");
return;
}
// 입력받은 값이 양수인지 판단.
if (iResult1 < 0 || iResult2 < 0)
{
MessageBox.Show("입력받는 값은 양수만 가능합니다.");
return;
} //걸러내는 부분을 먼저
for (int i = iResult1; i <= iResult2; i++)
{
if (i % iValue == 0)
{
iResult += i; // iResult = iResult + i;
}
}
MessageBox.Show(Convert.ToString(iResult));
}
}
}
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 For_문_실습_메소드_간추리기 : Form
{
public For_문_실습_메소드_간추리기()
{
InitializeComponent();
}
int iSum = 0;
private void button1_Click(object sender, EventArgs e)
{
TimesSum(2, textBox1.Text, textBox2.Text);
}
private void button2_Click(object sender, EventArgs e)
{
TimesSum(5, textBox1.Text, textBox2.Text);
}
private void button3_Click(object sender, EventArgs e)
{
TimesSum(10, textBox1.Text, textBox2.Text);
}
private void TimesSum(int times, string sStart, string sEnd)
{
string sValue1 = sStart;
string sValue2 = sEnd;
int iValue;
bool bCheck1 = int.TryParse(sValue1, out iValue);
bool bCheck2 = int.TryParse(sValue2, out iValue);
if (!bCheck1)
{
MessageBox.Show("숫자로 변환할 수 없는 값입니다.");
return;
}
else if (!bCheck2)
{
MessageBox.Show("숫자로 변환할 수 없는 값입니다.");
return;
}
else if ((Convert.ToInt32(sStart) < 0) || (Convert.ToInt32(sEnd) < 0))
{
MessageBox.Show("음수를 입력할 수 없습니다.");
return;
}
int iStart = Convert.ToInt32(sStart);
int iEnd = Convert.ToInt32(sEnd);
for (int i = iStart; i <= iEnd; i++)
{
if (i % times == 0)
{
iSum += i;
}
}
MessageBox.Show($"범위 내 {times}의 배수의 합은 {iSum} 입니다.");
return;
}
}
}
LIST