我无法理解返回值

时间:2013-03-20 01:13:26

标签: java return

所以对于这个程序,我必须使用一个方法,如果第一个圆圈较小则返回-1,如果两个圆圈具有相同的大小则返回0,或者如果第一个圆圈较大则返回1。根据此方法的返回值,main方法应打印一行输出,如:        绿色圆圈小于红色圆圈。

   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

希望这清楚......如果你们需要我发布我到目前为止的代码

2 个答案:

答案 0 :(得分:3)

您假设的compareTo方法“将”比较结果与(不是)特殊代码进行编码:

  • 当第一个圆圈较小时,需要返回-1
  • 当圈子相同时,需要返回0
  • 当第一个圆圈较大时,需要返回1

这意味着主方法应该对它进行解码:

  • 当它看到-1时,它必须打印“较小”,
  • 当它看到0时,它必须打印“相同”和
  • 当它看到1时,它必须打印“更大”。

所有这些都可以使用一系列if - then - 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");
}

答案 1 :(得分: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否则它将返回0 a == b }}