黑马程序员技术交流社区

标题: 如何打印心形? [打印本页]

作者: 201314    时间: 2016-12-1 23:16
标题: 如何打印心形?
我又做得哪里不对了,最右边的总是不对,哪个空格和心形曲线有什么关系吗?


private static void heartTwoWithXK(int r, double size, String center, String left, String right) {
  // TODO Auto-generated method stub
  
  size=1/(1.5*r*size);
  StringBuilder sb=new StringBuilder();
  //从上到下每一行都添加换行
  for (int y = r; y >=-r; y--,sb.append("\n"))
   
   
   
   //开始操作列
   for(int x=-2*r;x<=4*r;x++)
   {
   
    //画左边的心形
    boolean isLeft=inHeart(size,x,y+3);
    //画右边的心形
    boolean isRight=inHeart(size,x-25,y);
   
    //双空格
   
    String req=null;
    String w="";
   
    if(isLeft&&isRight)
     req=center;
    else if(isLeft)
     req=left;
    else if(isRight)
     req=right;
   
   
    //打印上下两行心形
    else if((y==-r||y==r))
    {
     
     if(x<3*r-7)
     {     
                    req="♥";
          w="  ";//双空格
     }     
    }
   
   
    else if(x==4*r||x==-2*r||line(x,y+3))req="♥";
   
    if(req!=null)sb.append((req+req).charAt((x-y)%req.length()+req.length())+w);
   
    else sb.append("   ");//双空格
   
   }
   
  
  
   System.out.println(sb.toString());
  
  
   
  
  }
  
private static boolean line(int x, int y) {
  return 4*y-x==0;
}
//

private static boolean inHeart(double size, int x, int y) {
  // TODO Auto-generated method stub
  return Math.pow(Math.pow(x*size,2)+Math.pow(y*2*size,2)-1,3)-Math.pow(x*size,2)*Math.pow(y*2*size, 3)<=0;
}






欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2