0
我使用gpdraw和pen.drawOval(num1,num2)創建了一個使用num1和num2的增量操作符爲5的球體的可視化表示。創建這個有我複製粘貼相同的兩行代碼40次。無論如何,這看起來似乎並不高效或專業。我如何重複這些行的確切次數或重複該方法的確切次數?在方法中擺脫複製粘貼代碼的方法
DrawSphereTester類
public class SphereDrawTester
{
public static void main(String[] args)
{
SphereDraw draw = new SphereDraw();
draw.draw();
}
}
DrawSphere類
import gpdraw.*;
public class SphereDraw
{
//Instance variables
public SketchPad Paper;
public DrawingTool pen;
//Constructor
public SphereDraw()
{
Paper = new SketchPad(800, 800);
pen = new DrawingTool(Paper);
}
public void draw()
{
int num1 = 0;
int num2 = 200;
pen.drawCircle(100);
pen.drawOval(num1,num2);
num1 += 5;
pen.drawOval(num1,num2);
num1 += 5;
pen.drawOval(num1,num2);
num1 += 5;
pen.drawOval(num1,num2);
num1 += 5;
pen.drawOval(num1,num2);
num1 += 5;
pen.drawOval(num1,num2);
num1 += 5;
pen.drawOval(num1,num2);
num1 += 5;
pen.drawOval(num1,num2);
num1 += 5;
pen.drawOval(num1,num2);
num1 += 5;
pen.drawOval(num1,num2);
num1 += 5;
pen.drawOval(num1,num2);
num1 += 5;
pen.drawOval(num1,num2);
num1 += 5;
pen.drawOval(num1,num2);
num1 += 5;
pen.drawOval(num1,num2);
num1 += 5;
pen.drawOval(num1,num2);
num1 += 5;
pen.drawOval(num1,num2);
num1 += 5;
pen.drawOval(num1,num2);
num1 += 5;
pen.drawOval(num1,num2);
num1 += 5;
pen.drawOval(num1,num2);
num1 += 5;
pen.drawOval(num1,num2);
num1 += 5;
pen.drawOval(num1,num2);
num1 += 5;
pen.drawOval(num1,num2);
num1 += 5;
pen.drawOval(num1,num2);
num1 += 5;
pen.drawOval(num1,num2);
num1 += 5;
pen.drawOval(num1,num2);
num1 += 5;
pen.drawOval(num1,num2);
num1 += 5;
pen.drawOval(num1,num2);
num1 += 5;
pen.drawOval(num1,num2);
num1 += 5;
pen.drawOval(num1,num2);
num1 += 5;
pen.drawOval(num1,num2);
num1 += 5;
pen.drawOval(num1,num2);
num1 += 5;
pen.drawOval(num1,num2);
num1 += 5;
pen.drawOval(num1,num2);
num1 += 5;
pen.drawOval(num1,num2);
num1 += 5;
pen.drawOval(num1,num2);
num1 += 5;
pen.drawOval(num1,num2);
num1 += 5;
pen.drawOval(num1,num2);
num1 += 5;
pen.drawOval(num1,num2);
num1 += 5;
pen.drawOval(num1,num2);
num1 += 5;
pen.drawOval(num1,num2);
num1 += 5;
pen.drawOval(num1,num2);
}
}
還沒有測試過這個,但它看起來很穩固=) – Barodapride 2014-09-19 04:12:48
謝謝了,有一種循環的想法。對編碼新手有缺點,比如不知道如何做簡單的事情,比如創建for循環。這將在未來幫助我很多,我已經知道了。 – giigii 2014-09-19 04:19:14