2016-03-31 57 views
-1

我正在製作左右箭頭的程序。它們是從ShapeInterface實現的ShapeBase類中擴展而來的。我沒有粘貼它們,所以它看起來不會太大。問題是我已經找到了正確的箭頭(我相信),但無法找到我的方式來獲得左箭頭。我一直感到困惑,似乎無法理解如何去做。任何幫助表示讚賞。 RightArrow Example Output如何製作Java中星號的左箭頭?

public abstract class ShapeBase implements ShapeInterface 
{ 
private int offset; 

public ShapeBase() 
{ 
    offset = 0; 
} 

public ShapeBase(int theOffset) 
{ 
    offset = theOffset; 
} 

public abstract void drawHere(); 

public void drawAt(int lineNumber) 
{ 
    for (int count = 0; count < lineNumber; count++) 
     System.out.println(); 
    drawHere(); 
} 

public void setOffset(int newOffset) 
{ 
    offset = newOffset; 
} 

public int getOffset() 
{ 
    return offset; 
} 
} 





public class RightArrow extends ShapeBase 
{ 
private int tail; 
private int width; 

public RightArrow() 
{ 
    super(); 
    tail = 0; 
    width = 0; 
} 

public RightArrow(int theOffset ,int tailSize, int theWidth) 
{ 
    super(theOffset); 
    tail = tailSize; 
    width = theWidth; 
    set(tail , width); 
} 
public void set(int newHeight, int newWidth) 
{ 
    tail = newHeight; 
    width = newWidth; 
} 

public void drawHere() 
{ 
    topArrowhead(); 
    ArrowTail(); 
    bottomArrowHead(); 
} 
public void topArrowhead() 
{ 
    skipSpaces(getOffset()); 
    System.out.println("*"); 

    for(int i = 0; i<width/2; i++) 
    { 
     skipSpaces(getOffset()); 
     System.out.print("*"); 
     skipSpaces(i); 
     System.out.println("*"); 
    } 
} 

// method to draw the arrow tail 
public void ArrowTail() 
{ 
    for(int count=0; count<tail; count++) 
    { 
     System.out.print("*"); 
    } 
    skipSpaces(tail+width); 
    System.out.print("*"); 

} 

// method to draw bottom of arrowhead 
public void bottomArrowHead() 
{ 
    for(int i =1;i<width/2; i--) 
    { 
     skipSpaces(getOffset()); 
     System.out.print("*"); 
     skipSpaces(i); 
     System.out.println("*"); 
    } 
    skipSpaces(getOffset()); 
    System.out.println("*"); 
} 

private static void skipSpaces(int number) 
{ 
    for (int count=0; count< number; count++) 
    System.out.print(" "); 
} 
} 

向左箭頭CLASS,我相信這是唯一的方法都需要改變。 我只是不知道如何

public void topArrowhead() 
     { 
      skipsSpaces(getOffset()); 
      System.out.println("*"); 

      for(int i = 0 i<width/2; i++) 
      { 
       skipSpaces(getOffset()); 
       System.out.print("*"); 
       skipSpaces(i); 
       System.out.println("*"); 
      } 
     } 

     // method to draw the arrow tail 
     public void ArrowTail() 
     { 

     } 

     // method to draw bottom of arrowhead 
     public void bottomArrowHead() 
     { 

     } 

     private static void skipSpaces(int number) 
     { 
      for (int count=0; count< number; count--) 
      System.out.print(" "); 
     } 
} 

回答

0

這是你如何畫一個左箭頭,它不使用你的方法(如你不ShapeBase提供源),但你可以看到下面如何執行其得到創造,然後可以以你自己的方式使用。

public class LeftArrow 
{ 
    private int tail=15; 
    private int width=7; 

    //Method to draw the left arrow  
    public void DrawArrow() 
    { 
     for(int i = 0,r=0; i<width; i++) 
     { 
      //for spaces before head 
      for(int j=0;j<(width/2)-r;j++) 
        System.out.print(" "); 

      for(int j=0;j<=r;j++) 
      { 
       if(j==r || j==0) 
       System.out.print("*"); 
       else 
       System.out.print(" ");//for spaces inside head 
      } 
      if(i==width/2)//to draw tail after the mid of head 
       ArrowTail(); 

      if(i<width/2)//controls the increment & decrements of spaces before head 
       r++; 
      else r--; 

      System.out.println(); 
     } 
    } 

    // method to draw the arrow tail 
    public void ArrowTail() 
    { 
     for(int count=0; count<tail; count++) 
     { 
      System.out.print("*"); 
     } 
    } 

    public static void main(String[] args) 
    { 
     LeftArrow Arrow = new LeftArrow(); 
     Arrow.DrawArrow(); 
    } 
} 
+0

謝謝你試圖幫助我。如果您可以再次查看它,我將ShapeBase類添加到了我的問題中,而不是已經完成了很多工作。 –

+0

有很多code.Better你看上面的代碼是如何工作的,那麼你可以實現你自己的'ArrowTail()'和'bottomArrowHead()'方法。 –

+0

好的,再次感謝 –