package ch03;

public class Work0529c
{
 //3. 출력결과 :
 /*
  * int result=sum(1,10);
  * 1+2+..+10. 같은결과, x가 큰경우는 하고경우만..
  * System.out.println("resut : "+result);
  * hint : 반복문
  * */
 /* 기존 방식.new Work0529c(1,10); 방식으로 바꾸기..
 public Work0529c()
 {
  int result = sum(1,10);
  System.out.print(result);
  System.out.println("\r\nresut : "+result);
 }
 */
 public int x;
 public int y;
 
 //public Work0529c()
 public Work0529c(int x, int y)
 {
  //기존에서 추가 ...
  this.x = x;
  this.y = y;
  //int result = sum(1,10);
  int result = sum();
  System.out.print(result);
  System.out.println("\r\nresut : "+result);
 }
 
 //public int sum(int x, int y)
 public int sum()
 {
  /*if(x>y)
  {
   int temp = x;
   x = y;
   y = temp;
  }*/
 
  if(this.x>this.y)
  {
   int temp = this.x;
   this.x = this.y;
   this.y = temp;
  }
 
  int result=0;
 
  /*do
  {
   result += x;
   System.out.print(x);
   if(x<y)
   {
    System.out.print("+");
   }
   else
   {
    System.out.print("=");
   }
   x++;
  }
  while(x<=y);*/
 
  do
  {
   result += this.x;
   System.out.print(this.x);
   if(this.x<this.y)
   {
    System.out.print("+");
   }
   else
   {
    System.out.print("=");
   }
   this.x++;
  }
  while(this.x<=this.y);
   
  return result;  
 }
 
 public static void main(String[] args)
 {
  //new Work0529c();
  //기존에서 생성자 매개변수 전달방식.
  new Work0529c(1,10);
 }
}

Posted by 말없제이
,