2013-03-20 2 views
-2

그래서 첫 번째 원이 더 작 으면 -1을 반환해야하는 메서드를 사용해야하고 두 원이 같은 크기 인 경우 0을 반환하거나 첫 번째 원이 더 큽니다. 이 메서드의 반환 값에 따라 기본 메서드는 다음과 같은 출력 줄을 인쇄해야합니다. 녹색 원이 빨간색 원보다 작습니다. 여기 반환 값을 이해하는 데 문제가 있습니다

but i dont know how to program it to where 

    if (r1 > r2) 
     return -1; 

    so that the main method prints 

    r1 is bigger then r2 



    BEFORE CHANGES this is what i had when i asked the question 




     import java .awt.*; // for the graphics classs 
     import java .util.*;// for scanner class 
     public class Circles{ 
     public static void main(String[] args){ 
     Scanner input = new Scanner(System.in); //scanner object 
     DrawingPanel panel=new DrawingPanel(400,300); 
     Graphics gObject = panel.getGraphics(); //grahics object 
     // get info on circles 
     System.out.println("please enter your coordnates for the blue circle"); 
     int x1=input.nextInt(); 
     int y1=input.nextInt(); 
     System.out.println("please enter your radius for the blue circle"); 
     int r1=input.nextInt(); 
     System.out.println("please enter your coordnates for the red circle"); 
     int x2=input.nextInt(); 
     int y2=input.nextInt(); 
     System.out.println("please enter your radius for the red circle"); 
     int r2=input.nextInt(); 
     System.out.println("please enter your coordnates for the pink circle"); 
     int x3=input.nextInt(); 
     int y3=input.nextInt(); 
     System.out.println("please enter your radius for the pink circle"); 
     int r3=input.nextInt(); 
     gObject.setColor(Color.BLUE); 
     drawCircle(gObject,x1 ,y1 ,r1); 
     gObject.setColor(Color.RED); 
     drawCircle(gObject,x2,y2,r2); 
     gObject.setColor(Color.PINK); 
     drawCircle(gObject,x3,y3,r3); 
     compare(r1,r2); 
     compare(r1,r3); 
     compare(r2,r3); 

     }//end of main 

     public static void drawCircle(Graphics g, int x1 , int y1 , int r1){ 
     int X1 = (x1-r1); 
     int Y1 = (y1 - r1); 

     g.fillOval(X1 , Y1 , 2*r1 , 2*r1); 
     }//end of drawcircle 
     //start of compare 
     public static void compare(int r1 , int r2){ 
     if (r1<r2){ 
     System.out.println("Second circle is bigger then The First"); 

     } 
     else if (r1 == r2){ 
      System.out.println("the circles are the same"); 

     } 
      else if (r1 > r2){ 
      System.out.println("Second is smaller then First"); 

      } 

     }} 

은 어떤 이유로 오류 증가에 대한

  import java .awt.*; // for the graphics classs 
     import java .util.*;// for scanner class 
     public class Circles{ 
     public static void main(String[] args){ 
     Scanner input = new Scanner(System.in); //scanner object 
     DrawingPanel panel=new DrawingPanel(400,300); 
     Graphics gObject = panel.getGraphics(); //grahics object 
      // get info on circles 
     System.out.println("please enter your coordnates for the blue circle"); 
     int x1=input.nextInt(); 
     int y1=input.nextInt(); 
     System.out.println("please enter your radius for the blue circle"); 
     int r1=input.nextInt(); 
     System.out.println("please enter your coordnates for the red circle"); 
     int x2=input.nextInt(); 
     int y2=input.nextInt(); 
     System.out.println("please enter your radius for the red circle"); 
     int r2=input.nextInt(); 
     System.out.println("please enter your coordnates for the pink circle"); 
     int x3=input.nextInt(); 
     int y3=input.nextInt(); 
     System.out.println("please enter your radius for the pink circle"); 
     int r3=input.nextInt(); 
      gObject.setColor(Color.BLUE); 
      drawCircle(gObject,x1 ,y1 ,r1); 
      gObject.setColor(Color.RED); 
     drawCircle(gObject,x2,y2,r2); 
     gObject.setColor(Color.PINK); 
     drawCircle(gObject,x3,y3,r3); 
     compare(r1,r2); 
      compare(r1,r3); 
     compare(r2,r3); 
     int cmpResult = compare(r1, r2); 
      if (cmpResult == -1) { 
     System.out.println("r1 is smaller then r2"); 
     } else if (cmpResult == 0) { 
     System.out.println("r1 and r2 are the same"); 
      } else { 
      System.out.println("r1 is bigger then r2"); 
     } 
      System.out.println("Second circle is bigger then The First"); 
      }//end of main 

     public static void drawCircle(Graphics g, int x1 , int y1 , int r1){ 
      int X1 = (x1-r1); 
     int Y1 = (y1 - r1); 

      g.fillOval(X1 , Y1 , 2*r1 , 2*r1); 
      }//end of drawcircle 
      //start of compare 
      public static int compare(int r1 , int r2, cmpResult){ 
     if (r1<r2){ 
     return -1; 

      } 
      else if (r1 == r2){ 
      return 0; 

     } 
      else if (r1 > r2){ 
      return 1; 

      } 

      } } 

을 heres 오류는 나에게주는 것을 수정 너희들이 나를 필요하면 희망 ... 명확 이잖아

 3 errors found: 
    File: J:\CS Projects\Circles.java [line: 49] 
    Error: Syntax error, insert "}" to complete ClassBody 
    File: J:\CS Projects\Circles.java [line: 51] 
    Error: Syntax error on token "cmpResult", VariableDeclaratorId expected after this token 
    File: J:\CS Projects\Circles.java [line: 65] 
    Error: Syntax error on token "}", delete this token 

내가 지금까지 가지고있는 코드를 게시하십시오

+1

에 대한 0을 반환합니다 a < b를 확인합니다! 무엇을 시도 했습니까? 그리고 어떤 문제가 발생 했습니까? –

답변

3

가상의 compareTo 메서드는 "enco DES "는 (그리) 특수 코드와 비교 결과 : 원이 동일한 경우 첫 번째 원이 작은 경우, 그것은 필요

  • -1를 반환 할 수는
  • , 그것은 반환해야 0
  • 첫 번째 원이 클 때 1을 반환해야합니다. 이 -1를 볼 때 0을 볼 때

    • 는 그것이 "는 인쇄해야,
    • 를"작은 "인쇄해야

    이 기본 방법은, 동일의 디코딩을 수행한다는 것을 의미 동일한 ",

  • 1을 볼 때"더 크게 "인쇄해야합니다. then - -

이 모든는 if의 순서로 수행 할 수 있습니다 else 문 :

int cmpResult = compareTo(r1, r2); 
if (cmpResult == -1) { 
    System.out.println("r1 is smaller then r2"); 
} else if (cmpResult == 0) { 
    System.out.println("r1 and r2 are the same"); 
} else { 
    System.out.println("r1 is bigger then r2"); 
} 
+0

'isBigger'는'bool'을 반환 할 것처럼 들립니다. 나는 당신이 자바 프로그램보다 관용적이기 때문에 그것을 '비교'할 수 있도록 이름을 바꿉니다. –

+0

그래서 내가 그 예제를 주에 배치하고 그것을 사용하려고하는 방법에 전달하고 나는이 오류를 recevied. 1 오류가 발견되었습니다 : 파일 : J : \ CS Projects \ Circles.java [line : 51] 오류 : 토큰 "cmpResult"에 구문 오류가 발생했습니다.이 토큰 다음에 VariableDeclaratorId가 필요합니다. 나는 잘못된 이름을 전달 했나? 내가 뭔가 잘못 했어. – user2172510

+0

@ user2172510 수정 된 코드로 질문을 업데이트하여 오류를 발견하거나 pastebin.com에 붙여 넣을 수 있고 링크를 게시 할 수 있습니다. – dasblinkenlight

2

a=5; b=6합니다. a>b 반환이처럼 쓸 수 있도록 boolean 값 :

int compare_radius(int a, int b) 
    { 
     return (a > b ? 1 : (a < b ? -1 : 0)); 
    } 

(condition)? val1 : val2ternary 연산자로라고합니다. 조건이 true 인 경우 val1, 그렇지 않으면 val2을 반환합니다. 이 사실은 다음 다른 -1을 반환하는 경우 다른 1을 반환 a > b 경우 귀하의 경우에는 그것은 정직하게, 그것은 그 명확하지 않다가 a == b

+0

! 그러나 이것이 무슨 일인지에 대한 설명을 추가하는 데 도움이 될 수도 있습니다. 그는 3 진 연산자 – ElefantPhace

+0

을 이해할 수 있을지 확신하지 못합니다. ^^ 올바른 것은 무엇입니까? – user2172510

+0

중첩 된 삼항 연산을 대괄호로 묶는 것이 좋습니다. 그렇지 않으면 망칠 것입니다. 그래서'(a> b? 1 : (a Serdalis

관련 문제