좋은질문

[C#] FOR 반복문 >> WHILE 반복문으로 바꾸는법좀 알려주세요 ㅠㅠ제발

JustineBaek 2014. 10. 4. 01:13

 

 

 

 

안녕하십니까 형님 누님들 제발 도와주세요 과제 마감일은 일요일인대 아는건 없군요 ㅠㅠ 형님 누님들 제발

흑흑 살려살려주세용 ㅠㅠ

static void Main(String[] args)
{
 for (int i =2; i < 10; i++)
{
   ConsoIe.writeLine("=== {0} 단 입니다. ===", i);
   for (int j = 1; j < 10; j++)
   {
      ConsoIe.WriteLine("{0} * {1} = {2}", i, j, i * j);
   }
  }
}

 

실행결과는  이렇게 됩니다     

****2단*****   ****3단****

2 * 1 = 2            3 * 1 = 3

2 * 2 = 4            3 * 2 = 6

.

.

.

.

이렇게 쭈쭈죽 2단에서 9단까지 구구단이 쭉 나오는겁니다 제발 알려주세용 ㅠㅠ 내공 통크게 드립니다!!

 

그리구 간단한 설명도 부탁드립니다 ㅠㅠ

 

 

 

안녕하세요?


1. 아래에 코드를 기술합니다.


static void Main(string[] args)
{

     //PrintFor();
     PrintWhile();
}

 

// for문 사용

static void PrintFor()
{
     // 2 -> 2 3    i+0 (i+0)+1
     // 3 -> 4 5    i+1 (i+1)+1 
     // 4 -> 6 7    i+2 (i+2)+1
     // 5 -> 8 9    i+3 (i+3)+1
     int count = 0;
     for (int i = 2; i < 6; i++)
    {
          Console.Write("****{0}단****", i + count);
          Console.Write("     ");
          Console.Write("****{0}단****", (i + count) + 1);
          Console.WriteLine();
         

          for (int j = 1; j < 10; j++)
        {
               Console.Write("{0} * {1} = {2}", (i + count), j, (i + count) * j);
               Console.Write("     ");
               Console.Write("{0} * {1} = {2}", (i + count) + 1, j, ((i + count) + 1) * j);
               Console.WriteLine();
          }
          Console.WriteLine();
          count++;
     }
}

 

// while문 사용

static void PrintWhile()
{
     // 2 -> 2 3    i+0 (i+0)+1
     // 3 -> 4 5    i+1 (i+1)+1 
     // 4 -> 6 7    i+2 (i+2)+1
     // 5 -> 8 9    i+3 (i+3)+1


     int i = 2;
     int count = 0;

     while (i < 6)
    {
          Console.Write("****{0}단****", i + count);
          Console.Write("     ");
          Console.Write("****{0}단****", (i + count) + 1);
          Console.WriteLine();


          int j = 1;
          while (j < 10)
        {
               Console.Write("{0} * {1} = {2}", (i + count), j, (i + count) * j);
               Console.Write("     ");
               Console.Write("{0} * {1} = {2}", (i + count) + 1, j, ((i + count) + 1) * j);
               Console.WriteLine();
               j++;
          }
          Console.WriteLine();

          count++;
         i++;

     }
}

 

2. 2단3단, 4단5단,.,8단9단 처럼 나오게 하기위해 아래의 방법을 구사하였습니다.

 

   -. i를 기준으로 보면 각행의 나란히 표시되는 단의 계산은 다음과 같습니다. 

      ; 2 -> 2 3    i+0 (i+0)+1
      ; 3 -> 4 5    i+1 (i+1)+1 
      ; 4 -> 6 7    i+2 (i+2)+1
      ; 5 -> 8 9    i+3 (i+3)+1

 

   -. while문에서는 밖에서 카운터 변수를 선언하고, 조건식이 거짓이 되면 탈출합니다.

      ; 단표시 i = 2; while(i < 6) ~~~

      ; 구구단 j = 1; while(j < 10) ~~~

 

   -. main함수에서 PrintFor(); 를 주석해제하면, 적절히 수정한 for문도 정상 동작합니다.

 

3. 실행결과

     

      

도움이 되시길 바랍니다.

감사합니다.




written by

  

JustineBaek

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

C과외/C#과외/VB과외 프로그래밍 과외.서울인천경기-방문강의.기타전국-화상강의.제대로 체계적으로 배우고싶은분들.15년경력 프리랜서.삼성/포스코/한진 등.

서울,인천,경기,분당,일산,과천,산본,부천,부평,평촌,안산,안양,광명,용인,시흥,성남,수원,평택,오산,화성,동탄,천안,아산,

c#프로그래밍1:1과외, 15년 이상 프로젝트 경력의 프리랜서 직접 강의,

c#프로그래밍 과외, c#프로그래밍교육, IT·컴퓨터·컴퓨터과외비·프로그래밍과외, c# 강좌, c# 프로그래밍,

데이터베이스, 데이터베이스 프로그래밍, db, DB, 과외비,컴퓨터1:1,컴퓨터개인지도,컴퓨터실무과외,컴퓨터과외,컴퓨터1:1과외,

서울,인천,경기,부천,안산,분당,일산,천안,과천,산본,안양,광명,평촌,수원,평택,오산,용인,부평,시흥,성남,화성,동탄

광주,부산,대구,대전,마산,창원,울산,전주,군산,원주,강릉,충청남도,충청북도,강원도,경상남도,경상북도,전라남도,전라북도,제주도