2016-03-31 1 views
-1

Je travaille sur un programme qui fait des flèches droite et gauche. Ils sont étendus à partir d'une classe ShapeBase qui est implémentée à partir d'une ShapeInterface. Je ne les ai pas collés pour que ça ne paraisse pas écrasant. Le problème est que j'ai compris la flèche droite (je crois) mais que je n'arrive pas à trouver la flèche de gauche. Je continue à être confus et ne peux pas sembler comprendre comment le faire. Toute aide est appréciée. RightArrow Example OutputComment faire une flèche gauche d'astérisques dans 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(" "); 
} 
} 

POUR LA GAUCHE DE CLASSE, je crois que ce sont les seules méthodes doivent changer. Je ne sais pas comment

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

Répondre

0

Voici comment vous pouvez dessiner une flèche gauche, il n'utilise vos méthodes (comme vous ne fourni source pour ShapeBase) mais vous pouvez voir ci-dessous la mise en œuvre de sa façon être créé et ensuite utiliser à votre manière.

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

Merci d'avoir essayé de m'aider. J'ai ajouté la classe ShapeBase à ma question si vous pouviez la revoir, je préfère ne pas effacer beaucoup de mon travail. –

+0

Il y a beaucoup de code. Plus vous voyez le code au-dessus comment cela fonctionne, alors vous pourriez être en mesure d'implémenter les méthodes 'ArrowTail()' & bottomArrowHead() 'de votre propre. –

+0

D'accord, merci encore –