嗨,我似乎无法打印一个程序的结果,该程序将使用DrawingPanel比较三个圆圈大小

时间:2016-10-17 17:56:23

标签: java if-statement drawing system.out

嗨,初学者程序员在这里我需要帮助打印这个程序的结果。我已经创建了一种比较圆半径的方法,但是我希望在打印结果时包含比较的圆的颜色,但我似乎无法做到,如(?)所示。我该如何打印不同的颜色?

例如:当我比较绿色和红色时  "绿色圆圈大于红色圆圈"  当我将绿色与蓝色进行比较时  "绿色圆圈小于蓝色圆圈"

 //class
 // main method
    //asked for input and stored values in variable for r1, x1, y1
    g.setColor(Color.GREEN);
    g.fillOval(x1-r1, y1-r1, r1*2, r1*2);
   //asked for input and stored values in variable for r2, x2, y2
    g.setColor(Color.RED);
    g.fillOval(x2-r2, y2-r2, r2*2, r2*2);

    //asked for input and stored values in variable for r3, x3, y3
    g.setColor(Color.BLUE);
    g.fillOval(x3-r3, y3-r3, r3*2, r3*2); 
    int compare = size(r1, r2);
    result1(compare);
    compare = size(r1, r3);
    result1(compare);
    compare = size(r2, r3);
    result1(compare);


public static int size(int r1, int r2){
    if(r1 < r2){
      return -1;
    }else if( r1 == r2){
      return 0;
    }else{
      return 1;
    }
  }
   public static void result1(int n){
    if (n == -1){
      System.out.println("The " + ? + "cirlce is smaller than the " + ? + "circle.");
    }else if(n == 0){
      System.out.println("The " + ? + "circle is the same size as the " + ? + "cirle.");
    }else{
      System.out.println("The " + ? + "circle is bigger than the " + ? + "circle");
    } 

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

创建一个类圈,如

public class MyCircle {
    int radius;
    Color color;

    public MyCircle(int radius, Color color) {
        this.radius = radius;
        this.color = color;
    }

    //Getter Setter...
    public int getRadius() {
        return radius;
    }

    public void setRadius(int radius) {
        this.radius = radius;
    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }
}

创建一个方法以将颜色名称作为字符串

public static String getColorAsName(MyCircle circle) {
    String colorName = null;
    if (circle.getColor().equals(Color.BLACK)) {
        colorName = "BLACK";
    } else if(circle.getColor().equals(Color.RED)) {
        colorName = "RED";
    }
    ...

    return colorName;
}

然后创建圆形对象,例如

MyCircle c1 = new Circle(r1, Color.GREEN)
MyCircle c2 = new Circle(r2, Color.GREEN)
etc... 

使用getter-setter可以在需要时更新属性。

更改方法

public static void result1(int n){

public static void result1(int n, MyCircle firstCircle, MyCircle lastCircle){
    if (n == -1){
        System.out.println("The " + getColorAsName(firstCircle) + " cirlce is smaller than the " + getColorAsName(lastCircle) + " circle.");
...

并致电result1(),如:

result1(compare, c1, c2);
...

答案 1 :(得分:0)

你需要一个像这样的方法:

public static void result1(int number, int radius1, int radius2){
    // some code
}