Java 我无法理解返回值

Java 我无法理解返回值,java,return,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

因此,对于这个程序,我必须使用一种方法,如果第一个圆较小,则返回-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");

            }

       }} 

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

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

  • 当第一个圆较小时,它需要返回
    -1
  • 当圆圈相同时,它需要返回
    0
  • 当第一个圆较大时,它需要返回
    1
这意味着主方法应执行相同的解码:

  • 当它看到
    -1
    时,它必须打印“更小”
  • 当它看到
    0
    时,它必须打印“相同”,并且
  • 当它看到
    1
    时,必须打印“更大”
所有这一切都可以通过一系列
语句来完成,如果
-
那么
-
否则
语句:

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");
}

如果
a=5;b=6
a>b
返回
boolean
值,因此您可以这样编写:

    int compare_radius(int a, int b)
    {  
        return (a > b ? 1 : (a < b ? -1 : 0));
    }
int比较_半径(int a,int b)
{  
返回值(a>b1:(a

(条件)?val1:val2
称为
三元运算符。如果条件为
true
则返回
val1
else
val2
。在你的例子中,如果
a>b
它返回
1
,否则它将检查
a
,如果这是真的,那么它将返回
-1
,否则它将返回
0
对于
a==b

,老实说,这不是那么清楚!您尝试了什么,遇到了什么问题?
isbiger
听起来好像会返回一个
bool
。如果我是你,我会将其重命名为
compare
,因为这是Java程序的惯用用法。因此,我将该示例放在main中,并将其传递给将要使用它的方法,我收到了此错误。发现1个错误:文件:J:\CS Projects\Circles.java[行:51]错误:标记“cmpResult”上的语法错误,此标记后应为VariableDeclaratorId。我把名字传错了吗?我做了别的事wrong@user2172510请用修改后的代码更新您的问题,这样我们就可以发现错误,或者粘贴到pastebin.com,并发布一个链接。我用原始代码和更新代码对其进行了更新,使事情变得更简单clearer@user2172510此
公共静态int比较(int r1、int r2、cmpResult)
应该是这样的:
公共静态int比较(int r1,int r2)
。非常简单!然而,这可能有助于澄清它的作用,我不确定他是否会理解三元运算符^^^是正确的。不知道这做了什么。在嵌套的三元运算周围放上括号也很好,如果不这样做,有时会出错。所以只要换成
(a>b1:(a
从可读性的角度来看,我也喜欢它。我理解这是什么,谢谢
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");
}
    int compare_radius(int a, int b)
    {  
        return (a > b ? 1 : (a < b ? -1 : 0));
    }