2013-03-20 76 views
-2

因此對於此程序,我必須使用一個方法,如果第一個圓圈較小,則返回-1;如果兩個圓圈的大小相同,則返回0;如果第一個圓圈返回1,則返回1更大。根據此方法的返回值,主要方法應打印一行輸出,如: 綠色圓圈小於紅色圓圈。我無法理解返回值

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; 

      } 

      } } 

繼承人的錯誤,它給我

 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

這並不是說清楚,說實話!你嘗試了什麼,你遇到了什麼問題? – 2013-03-20 01:16:05

回答

3

您的假設compareTo方法「enco德」與(不那麼)特殊代碼的比較結果:

  • 在第一圈是小,它需要返回-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'。如果我是你,我會將它重新命名爲「比較」,因爲這更像Java程序的習慣。 – 2013-03-20 01:29:00

+0

所以我把這個例子放在main中,並將它傳遞給將要使用它的方法,並且我收到了這個錯誤。找到1個錯誤: File:J:\ CS Projects \ Circles。java [line:51] 錯誤:令牌「cmpResult」上的語法錯誤,該令牌之後預期爲VariableDeclaratorId。我是否通過了錯誤的名稱?我是否做了其他錯誤 – user2172510 2013-03-20 01:46:11

+0

@ user2172510請使用您的修改代碼更新您的問題,以便我們發現錯誤或粘貼到pastebin.com併發布鏈接。 – dasblinkenlight 2013-03-20 01:53:35

2

如果a=5; b=6a>b回報boolean值,以便你可以這樣寫:

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

(condition)? val1 : val2被稱爲ternary操作。如果條件是true它返回val1否則val2。在如果a > b返回1否則你的情況下,它會檢查a < b,如果這是真的,然後返回-1否則將返回0a == b

+0

好看又簡單!然而,它可能有助於補充一些關於這是什麼的澄清,我不確定他會理解三元運算符 – ElefantPhace 2013-03-20 01:50:45

+0

^^是正確的不知道什麼是 – user2172510 2013-03-20 01:51:39

+0

它也很好地圍繞嵌套三元運算放置括號,那裏如果你不這樣做的話,它會讓你感到沮喪。所以只要改成'(a> b?1:(a Serdalis 2013-03-20 02:02:52