0
我必须打印一个圆(将半径,圆心(cx和cy)的中心坐标以及必须绘制的字符作为输入)。用字符打印圆和xy轴JAVA
我为轴和圆写了一系列if块。如果单独使用它们,它们会很好地工作,但是当我将它们放在同一个方法中(我只有一种方法)时,它们会以不希望的方式重叠。
注意:如果字符与轴重叠,则字符具有优先权。
谢谢你的帮助!
public static void drawCircle (int radius, int cx, int cy, char symbol){
//this method verifies that there are no negative values involved(throws error/exception
verifyInput(radius,cx,cy);
//set the values for extension of the axes (aka how long are they)
int xMax = cx+radius+1;
int yMax = cy+radius+1;
for(int j=yMax; j>=0; j--){
for(int i=0; i<=xMax; i++){
//set of if-block to print the axes
if (i == 0 && j == 0){
System.out.print('+');
}
else if(i == 0){
if (j == yMax){
System.out.print('^');
}
if(j != yMax){
System.out.println('|');
}
}
else if(j == 0){
if(i == xMax){
System.out.println('>');
}
if(i != xMax){
System.out.print('-');
}
}
//if block to print the circle
if(onCircle(radius,cx,cy,i,j)==true){
System.out.print('*');
}
else{
System.out.print(' ');
}
}
//I'm not sure if I need to use the print here V
//System.out.println()
}
}
这里是onCircle方法;它验证了每个i,如果j它是圆的轮廓绘制
public static boolean onCircle (int radius, int cx, int cy, int x, int y){
boolean isDrawn = false;
if(Math.pow(radius,2)<=(Math.pow((x-cx),2)+Math.pow((y-cy),2)) && (Math.pow((x-cx),2)+Math.pow((y-cy),2))<=(Math.pow(radius,2)+1)){
isDrawn = true;
}
return isDrawn;
}
这里是我的输出(没有最后的print语句:
^ |
*** |
* * |
* * |
* * + - - - - -*-*-*- >
这里是我的输出(与上次print语句
^
|
***
|
* *
|
* *
|
* *
+ - - - - -*-*-*- >
这是我应该得到: enter image description here
我尝试了你的建议,它绝对减少了角色之间的间距,但我仍然需要弄清底部星星的位置。我有一些麻烦,在我需要的条件下以及按照什么顺序包扎我的头脑。 –
先从一个方块开始。然后尝试在广场内建立一个圆圈。 – Sedrick
一般情况下,打印到System.out时不会覆盖,因此无法打印轴,然后用圆圈覆盖。如果您尝试这样做会发生什么,只是将它们打印出来,并且线上的字符比应该有的更多。当打印出轴的字符时,您需要检查该位置是否是“onCircle”,如果是,则不要打印轴字符。 – jwaddell