gpt4 book ai didi

java - 您好,我似乎无法打印使用 DrawingPanel 比较三个圆圈大小的程序的结果

转载 作者:行者123 更新时间:2023-12-01 09:22:20 25 4
gpt4 key购买 nike

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

例如:当我比较绿色和红色时 “绿色圆圈比红色圆圈大” 当我比较绿色和蓝色时 “绿色圆圈比蓝色圆圈小”

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

感谢您的帮助。

最佳答案

创建一个类(class)圈子,例如

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);
...

关于java - 您好,我似乎无法打印使用 DrawingPanel 比较三个圆圈大小的程序的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40092750/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com