2023. 10. 5. 20:03ㆍIT/C#
// 연습문제 7
using System.ComponentModel.DataAnnotations;
{ // 1. 이름 입력하기
// -최초 메시지 출력 - “`이름을 입력해주세요. (3~10글자)`”
// -이름이 3글자 미만, 10글자 초과라면 - “`이름을 확인해주세요.`”
// -올바르게 입력했다면 - “`안녕하세요! 제 이름은 xxx 입니다.`”
// -문자열의.Length 기능 을 이용하면 현재 문자열이 몇글자인지 알 수 있습니다.
/* Console.WriteLine("이름을 입력해주세요. (3 ~ 10글자)");
string input = Console.ReadLine();
int length = input.Length;
if (3 > length && length > 10)
{
Console.WriteLine("이름을 확인해주세요.");
}
else
{
Console.WriteLine("안녕하세요! 제 이름은 " + input + " 입니다.");
}
*/
}
{ // 2. 조건에 맞을때 까지 이름 입력
// 1번의 프로그램을 작성하면 3~10글자의 이름을 입력하지 않았을때 `이름을 확인해주세요.` 메시지 이후 프로그램이 종료됩니다.
// 이름을 올바르게 입력할때까지 실행되도록 적용해보세요.
// - 반복문과 bool 을 이용하여 만들 수 있습니다.
/* bool isSuccess;
do
{
Console.WriteLine("이름을 입력해주세요. (3 ~ 10글자)");
string input = Console.ReadLine();
int length = input.Length;
if (length >= 3 && length <= 10)
{
Console.WriteLine("안녕하세요! 제 이름은 " + input + " 입니다.");
}
else
{
Console.WriteLine("이름을 확인해주세요");
}
isSuccess = length >= 3 && length <= 10;
}
while (!isSuccess);*/
}
{ // 3. 반복시 기존 내용 지우기
// 2번의 프로그램을 작성하면 매번 새로운 텍스트가 생기게 됩니다.
// Console.Clear(); 기능을 활용하면 기존에 Console 에 표시되던 메시지를 지울 수 있습니다.
bool isSuccess;
do
{
Console.WriteLine("이름을 입력해주세요. (3 ~ 10글자)");
string input = Console.ReadLine();
int length = input.Length;
Console.Clear();
if (length >= 3 && length <= 10)
{
Console.WriteLine("안녕하세요! 제 이름은 " + input + " 입니다.");
}
else
{
Console.WriteLine("이름을 확인해주세요");
}
isSuccess = length >= 3 && length <= 10;
}
while (!isSuccess);
}