-3

したがって、このプログラムでは、最初の円が小さい場合は -1 を返し、2 つの円が同じサイズの場合は 0 を返し、最初の円が大きい場合は 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

うまくいけば、それは明らかです...私がこれまでに持っているコードを投稿する必要がある場合は、そうします

4

2 に答える 2

3

あなたの仮説的compareToな方法は、比較結果を(そうではない)特別なコードで「エンコード」します:

  • 最初の円が小さい場合は、戻る必要があります-1
  • 円が同じ場合は、戻る必要があります0
  • 最初の円が大きい場合は、を返す必要があります1

これは、mainメソッドが同じもののデコードを実行する必要があることを意味します。

  • それが見えるとき-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");
}
于 2013-03-20T01:18:37.627 に答える
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ます。condition の場合はelsetrueを返します。あなたの場合、 elseが返された場合はチェックされます。これが true の場合は、else が返されます。val1val2a > b1a < b-10a == b

于 2013-03-20T01:48:09.703 に答える