黑马程序员技术交流社区

标题: 周三活动小程序!!! [打印本页]

作者: 古银平    时间: 2012-11-7 09:33
标题: 周三活动小程序!!!
本帖最后由 古银平 于 2012-11-7 11:06 编辑

通过API文档查询Math方法类,打印如下所示近似圆。(绝对要自己写,练习自己的查阅文档能力,和思考能力,百度的不给分)记得贴结果
                           **
                  *                  *
               *                        *
             *                            *
           *                                *
          *                                  *
           *                                *
            *                            *
              *                        *
                 *               *
                        **

   
作者: 罗宝    时间: 2012-11-7 09:43
先来排队!
作者: 王震阳老师    时间: 2012-11-7 09:50
  1. import java.math.BigDecimal;

  2. public class Circle
  3. {
  4. /**
  5. * 半径
  6. */
  7. public int radius;

  8. /**
  9. * 一排点数 = 半径*2+1
  10. */
  11. public double point;

  12. public static void main(String[] args)
  13. {
  14. Circle circle = new Circle(6);
  15. circle.show();
  16. }

  17. Circle(double dou)
  18. {
  19. radius = changeValue(dou);//四舍五入
  20. point = radius*2 +1;//一排点数 = 半径*2+1
  21. }

  22. /**
  23. * 打印圆
  24. *
  25. */
  26. private void show()
  27. {
  28. for(int i = 0;i<point;i++)
  29. {
  30. for(int j = 0;j<point;j++)
  31. {
  32. if(isCircle(j,i))
  33. System.out.print(" *");
  34. else
  35. System.out.print(" ");
  36. }
  37. System.out.print("\n");
  38. }
  39. }

  40. /**
  41. * 判断某点是否是圆的点
  42. * @return true:是;false:不是
  43. */
  44. private boolean isCircle(int x, int y)
  45. {
  46. x = x-radius;
  47. y = y-radius;
  48. double temp = square(radius) - square(y);
  49. int z = ((int)Math.sqrt(temp)); // 用圆的公式求星号离x轴的长度
  50. if(x == z || x== -z)
  51. {
  52. return true;
  53. }
  54. else
  55. {
  56. return false;
  57. }
  58. }

  59. /**
  60. * 四舍五入
  61. * @param dou double
  62. * @return double
  63. */
  64. public int changeValue(double dou)
  65. {
  66. BigDecimal bdBigDecimal = new BigDecimal(String.valueOf(dou)).setScale(0, BigDecimal.ROUND_HALF_UP);
  67. return bdBigDecimal.intValue();
  68. }
  69. /**
  70. * 求平方
  71. * @param x int
  72. * @return 平方值
  73. */
  74. public double square(int x)
  75. {
  76. return (double)(x*x);
  77. }
  78. }
复制代码

作者: 〆_┌♂    时间: 2012-11-7 10:07
求关注  !
作者: 郑传庆    时间: 2012-11-7 10:08
{:soso_e113:}抢个位置围观
作者: 葬天    时间: 2012-11-7 10:11
占个位置~~学习去
作者: 王永荣    时间: 2012-11-7 10:42
本帖最后由 王永荣 于 2012-11-7 11:13 编辑

思路:

实现代码如下:
  1. public class PaintCircleDemo{
  2.         public static void main(String[] args){
  3.                 Circle c = new Circle(10);
  4.                 c.paint();
  5.         }
  6. }
  7. class Circle{
  8.         private int radius;
  9.         Circle(int r){
  10.                 radius = r;
  11.         }
  12.         public void paint(){
  13.                 int x,y,z;
  14.                 for(y=0;y<=2*radius;y++){
  15.                         x = radius-(int)Math.round(Math.sqrt(Math.pow(radius,2)-Math.pow(y-radius,2)));
  16.                         print(space(x));
  17.                         print("*");
  18.                         z = radius-x;
  19.                         print(space(2*z));
  20.                         System.out.println("*");
  21.                 }
  22.         }
  23.         public String space(int x){
  24.                 StringBuffer sb = new StringBuffer();
  25.                 for(int i =0;i<=x;i++)
  26.                         sb.append(" ");
  27.                 return sb.toString();
  28.         }
  29.         public void print(Object o){
  30.                 System.out.print(o);
  31.         }
  32. }
复制代码
呃。打印结果:这尼玛是圆啊。。。与预期相差太大了吧。。







作者: 陶辰    时间: 2012-11-7 11:02
package com.ttc;
import java.math.*;
public class test1 {
        public static void main(String []args){
                test1 tt =new test1();
               
                tt.Show();
        }
       
        //打印园
        public void Show(){
               
                double y,x,m;
               
               
                for(y=10;y>=-10;y--)
                {
                        m=2*Math.sqrt(100-y*y);
                        //计算行y对应的列坐标m,行距大于列距,不进行调节显示出来的将是椭圆电脑屏幕长宽不一样调整为2最好
               
                        for(x=1;x<30-m;x++){
                                System.out.print(" ");
                        }
                        //图形左侧空白控制
                        System.out.print("*");
                        //圆的左侧
                        for(;x<30+m;x++){
                                System.out.print(" ");
                        }
                        //图形的空心部分控制
                        System.out.print("*\n");
                        //圆的右侧
                }
        }
}

这是我大一时在一本书《c语言趣味算法百例》看到的题目 好像用c语言编的



作者: 陶辰    时间: 2012-11-7 11:10
运行结果

无标题.png (20.8 KB, 下载次数: 63)

无标题.png

作者: 朱宏青    时间: 2012-11-7 12:55
呜 这个简单 等我吃了饭再来~
作者: 打工人    时间: 2012-11-7 13:00
朱宏青 发表于 2012-11-7 12:55
呜 这个简单 等我吃了饭再来~

你丫敢不敢重新回复
作者: 朱宏青    时间: 2012-11-7 13:03
冯海霞 发表于 2012-11-7 13:00
你丫敢不敢重新回复

既然冯大同志要求了,那到时候重新回复吧...奶奶的 又是1分 我这个程序绝对比一般的给力多了 主要是用起来好用!哼~
作者: 石琪    时间: 2012-11-7 16:40
/*
        需求:定义一个函数,输入半径打印出以“*”组成的类圆形
*/
import java.lang.Math;

class shiqi2
{
        public static void main(String[] args)
        {
                Circle(20);               
        }
        public static void Circle(int R)
        {
                int r=R;
                int y=r;
                int x=-r;
                for( y=r;y>=-r;y--)
                {
                for( x=-r;x<=r;x++)
                {                       
                        if(Math.abs(x)==(int)Math.sqrt(r*r-Math.abs(y*y)))//判断此x轴坐标是否为圆上一点
                        System.out.print("#");
                        else
                        System.out.print(" ");                       
                }
                        System.out.print("\n");
                }                       
       
        }

}C:\Documents and Settings\Administrator\桌面
作者: 石琪    时间: 2012-11-7 16:49
补个图

shiqi.jpg (1.65 MB, 下载次数: 86)

shiqi.jpg

作者: 小学生0987    时间: 2012-11-7 18:35
画圆将点存入数组
package circle;
public class drawcircle {
    static boolean[][] a = new boolean[101][101];
    public void init()
    {
            for(int i=0;i<101;i++)
            {
                    for(int j=0;j<101;j++)
                {
                        a[i][j]=false;
                }
            }
    }
    public drawcircle()
    {
            init();
            float x=0,y=50;
            float d=(float) (1.25-50);
            a[(int)x+50][(int)y+50]=true;
            a[(int)y+50][(int)x+50]=true;
            a[(int)-x+50][(int)-y+50]=true;
            a[(int)-y+50][(int)-x+50]=true;
            a[(int)x+50][(int)-y+50]=true;
            a[(int)-x+50][(int)y+50]=true;
            a[(int)-y+50][(int)x+50]=true;
            a[(int)y+50][(int)-x+50]=true;
                while(x<=y)
                {
                        if(d<0){
                                d+=2*x+3;}
                        else
                        {
                                d+=2*(x-y)+50;
                                y--;
                        }
                        x++;
                        a[(int)x+50][(int)y+50]=true;
                    a[(int)y+50][(int)x+50]=true;
                    a[(int)-x+50][(int)-y+50]=true;
                    a[(int)-y+50][(int)-x+50]=true;
                    a[(int)x+50][(int)-y+50]=true;
                    a[(int)-x+50][(int)y+50]=true;
                    a[(int)-y+50][(int)x+50]=true;
                    a[(int)y+50][(int)-x+50]=true;
                       
                }
        }
}
将数组打印出来
package circle;
public class print {
        public print()
        {
                String s;
                drawcircle dr=new drawcircle();
                for(int i=0;i<101;i++)
                {
                        s="";
                        for(int j=0;j<101;j++)
                        {
                                if(dr.a[i][j])
                                {
                                        s=s+"*";
                                }
                                else
                                {
                                        s=s+" ";
                                }
                        }
                        System.out.println(s);
                }
        }
        public static void main(String[] args) {
        print p=new print();
        }
}
                                    
初学者,不会调用画圆方法,直接写了一个,所以代码有些多。
作者: 小学生0987    时间: 2012-11-7 18:40
下面是结果
                                          ***************                                          
                                        ***               ***                                       
                                      **                     **                                      
                                    **                         **                                    
                                   *                             *                                   
                                 **                               **                                 
                                *                                   *                                
                               *                                     *                              
                              *                                       *                              
                             *                                         *                             
                            *                                           *                           
                           *                                             *                           
                          *                                               *                          
                         *                                                 *                        
                        *                                                   *                        
                       *                                                     *                       
                      *                                                       *                     
                     *                                                         *                     
                    *                                                           *                    
                   *                                                             *                  
                  *                                                               *                  
                 *                                                                 *                 
                *                                                                   *               
               *                                                                     *               
              *                                                                       *              
             *                                                                         *            
            *                                                                           *            
           *                                                                             *           
          *                                                                               *         
         *                                                                                 *         
        *                                                                                   *        
       *                                                                                     *      
      *                                                                                       *      
     *                                                                                         *     
     *                                                                                         *     
    *                                                                                           *   
   *                                                                                             *   
   *                                                                                             *   
  *                                                                                               *  
  *                                                                                               *  
*                                                                                                 *
*                                                                                                 *
*                                                                                                 *
*                                                                                                   *
*                                                                                                   *
*                                                                                                   *
*                                                                                                   *
*                                                                                                   *
*                                                                                                   *
*                                                                                                   *
*                                                                                                   *
*                                                                                                   *
*                                                                                                   *
*                                                                                                   *
*                                                                                                   *
*                                                                                                   *
*                                                                                                   *
*                                                                                                   *
*                                                                                                 *
*                                                                                                 *
*                                                                                                 *
  *                                                                                               *  
  *                                                                                               *  
   *                                                                                             *   
   *                                                                                             *   
    *                                                                                           *   
     *                                                                                         *     
     *                                                                                         *     
      *                                                                                       *      
       *                                                                                     *      
        *                                                                                   *        
         *                                                                                 *         
          *                                                                               *         
           *                                                                             *           
            *                                                                           *            
             *                                                                         *            
              *                                                                       *              
               *                                                                     *               
                *                                                                   *               
                 *                                                                 *                 
                  *                                                               *                  
                   *                                                             *                  
                                 
作者: 罗宝    时间: 2012-11-7 18:41
本帖最后由 罗宝 于 2012-11-7 19:21 编辑

总算做出来啦!show一下!


file:///D:/QQ截图.png

代码如下:
  1. /**
  2. * 通过API文档查询Math方法类,打印如下所示近似圆。
  3. * (绝对要自己写,练习自己的查阅文档能力,和思考能力,百度的不给分)记得贴结果
  4. */

  5. package com.test3;

  6. import java.lang.Math;

  7. public class Test
  8. {
  9.         
  10.           public static void main(String args[])
  11.             {
  12.                           Test test=new Test();
  13.                           test.setR(6);
  14.                           test.circle();
  15.             }
  16.             
  17.     //定义圆的半径
  18.     private double r;

  19.     public double getR() {
  20.                 return r;
  21.         }

  22.         public void setR(double r) {
  23.                 this.r = r;
  24.         }


  25.         public void circle()
  26.     {

  27.         int x1,x2,y;
  28.         
  29.         String str;
  30.         for (int i = 0; i <= getR()*2; i++)
  31.         {
  32.             if (i == 0)
  33.             {
  34.                 y = Math.round((float)(r-Math.cos(Math.toRadians(15*i))*r));
  35.             }else{
  36.                 y = Math.round((float)(r-Math.cos(Math.toRadians(15*i))*r))-Math.round(
  37.                                 (float)(r-Math.cos(Math.toRadians(15*i-15))*r));
  38.             }
  39.             x1 = Math.round((float)(r-Math.sin(Math.toRadians(15*i))*r));
  40.             x2 = Math.round((float)(r+Math.sin(Math.toRadians(15*i))*r));
  41.             str = "";
  42.             if (i != 0 && i != getR()*2)
  43.             {
  44.                 if (y == 0)
  45.                 {
  46.                     continue;
  47.                 }
  48.             }else if(i == getR()*2){
  49.                 System.out.println("");
  50.             }
  51.             for (int j=0; j <y; j++)
  52.             {
  53.                 System.out.println("");
  54.             }
  55.             for (int k=0; k < x1; k++)
  56.             {
  57.                 str+=" ";
  58.             }

  59.             str+="*";

  60.             for (int k=0; k < x2-x1; k++)
  61.             {
  62.                 str+=" ";
  63.             }
  64.             
  65.             str+="*";
  66.             System.out.println(str);

  67.         }
  68.         System.out.println("");
  69.         
  70.     }

  71.   
  72. }


复制代码

作者: 罗宝    时间: 2012-11-7 19:02
本帖最后由 罗宝 于 2012-11-7 19:22 编辑

结果如下:

QQ截图.png (26.41 KB, 下载次数: 55)

QQ截图.png

作者: 杜正冬    时间: 2012-11-12 00:06
呵呵呵 这哥可以把

未命名.jpg (180.95 KB, 下载次数: 47)

海霞你看看是不是这个

海霞你看看是不是这个

作者: 杜正冬    时间: 2012-11-12 00:07
帅呆了。。。。。。




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