円を印刷する必要があります(半径、円の中心の座標(cxとcy)、描画する文字)を入力する必要があります。文字を含む円とxy軸を印刷するJAVA
軸と円の一連のifブロックを書きました。私はそれらを別々に使用するとうまくいきますが、同じ方法(私は1つの方法しか持たない)に入れてしまうと、望ましくない方法で重なります。
注:文字が軸と重なっている場合は、文字が優先されます。
ありがとうございました!
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メソッドです。
ここ^ |
*** |
* * |
* * |
* * + - - - - -*-*-*- >
:それはここでは
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文なし(私の出力であるすべてのI、jについて検証し私の出力は、最後のprint文で(ある
^
|
***
|
* *
|
* *
|
* *
+ - - - - -*-*-*- >
ここで私が得るべきである: enter image description here
私はあなたの提案を試みましたが、それは間違いなく文字間の余分なスペースを減らしましたが、まだ底の星の移動を把握する必要があります。私は、私が必要としている条件のどんな順番で私の頭を包んでいくのか少し問題があります。 –
最初に正方形で始まります。その後、正方形内に円を描こうとします。 – Sedrick
一般に、System.outに印刷する場合、上書きはないので、軸を印刷して円で上書きすることはできません。これを試してみると、それだけで両方が印刷され、ライン上にはもっと多くの文字があるはずです。軸の文字を印刷するときは、その位置が 'onCircle 'であるかどうかをチェックする必要があります。もしそうなら、軸の文字を表示しないでください。 – jwaddell