A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© yefeidd 中级黑马   /  2015-9-26 18:18  /  3880 人查看  /  16 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

10黑马币
题目:打印一个边长为N的中空的正三角形 注:N是控制台输入的参数,并根据参数打印星型。比如输入5会得到如下的图形。
如下:
            *
           * *
          *   *
         *     *
        * * * * *


16 个回复

倒序浏览
本帖最后由 太子奕 于 2015-9-26 19:54 编辑
  1. import java.util.Scanner;

  2. public class Test
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                 Scanner sc = new Scanner(System.in);
  7.                
  8.                 System.out.println("请输入一个整数:");

  9.                 int num = sc.nextInt();

  10.                 picture(num);

  11.         }       

  12. public static void picture (int n)
  13.         {
  14.                 for(int i = 0; i < n; i++)
  15.                 {       
  16.                         for(int j = 0; j < (2*n -1); j++)
  17.                         {       
  18.                                 if(i < n-1)                                                                                         //打印0~(n-1)的'*',不包括n-1行
  19.                                         {
  20.                                                 if(j ==(n - i -1)||j == (n + i -1))
  21.                                         {       
  22.                                                 System.out.print('*');
  23.                                         }
  24.                                         else
  25.                                         {       
  26.                                                 System.out.print(' ');
  27.                                         }
  28.                                 }
  29.                                 else                                                                                                         //打印n-1行的'*',注意是间隔一个空格打印一个星号
  30.                                 {       
  31.                                         if(j%2==0) System.out.print('*');                                       
  32.                                         else System.out.print(' ');
  33.                                 }
  34.                         }
  35.                         System.out.println();                                                                                //打印换行符
  36.                 }
  37.         }

  38. }
复制代码

回复 使用道具 举报
补充:必须在主函数外写一个方法来实现。注意每一边的*的个数是相等的
回复 使用道具 举报

          
       
                   打印三角形行数
         */
        static void triangleHollow(int line) {
                // 控制打印行数
                for (int i = 1; i <= line; i++) {
                        // 控制打印空格
                        for (int j = 1; j <= line - i; j++) {
                                System.out.print(" ");
                        }
                        // 控制打印*
                        for (int k = 1; k <= 2 * i - 1; k++) {
                                // 第一行,最后一行全部打印*
                                if (i == 1 || i == line) {
                                        System.out.print("*");
                                } else {
                                        // 第一个和最后一个打印*其余打印空格
                                        if (k == 1 || k == 2 * i - 1) {
                                                System.out.print("*");
                                        } else {
                                                System.out.print(" ");
                                        }
                                }
                        }
                        System.out.println();
                }

        }
}
回复 使用道具 举报
看看写的行吗! 有详细注释!

空的正三角.zip

669 Bytes, 下载次数: 172

评分

参与人数 1黑马币 +3 收起 理由
yefeidd + 3 很给力!但是没有按照要求

查看全部评分

回复 使用道具 举报
周伍阳 发表于 2015-9-26 19:00
打印三角形行数
         */
        static void triangleHollow(int line) {

你的打印是有问题的,每一边的星号是相等的
回复 使用道具 举报
import java.util.Scanner;

public class Test {

public void show(int n) {

int[][] nn = new int[n][]; // 根据输入值设置二维数组的第一维数
for (int i = 0; i < nn.length; i++) {
nn[i] = new int[i + 1]; // 设置第二维数
nn[i][0] = nn[i][i] = 1; // 对两头的元素赋值为1
for (int j = 1; j < nn[i].length - 1; j++) { // 对中间的元素赋值
nn[i][j] = nn[i - 1][j - 1] + nn[i - 1][j];
}
}

// 打印杨辉三角
for (int i = 0; i < nn.length; i++) {
for (int k = 0; k < n - i - 1; k++) {
System.out.print(" ");
}
System.out.print(nn[i][0] + " ");
for (int j = 1; j < nn[i].length; j++) {
if (nn[i][j] < 10) {
System.out.print(" " + nn[i][j] + " ");
} else if (nn[i][j] < 100) {
System.out.print(" " + nn[i][j] + " ");
} else {
System.out.print(" " + nn[i][j] + " ");
}
}
System.out.println();
}
System.out.println();
}

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("输入一个数字(n):");
int n = in.nextInt();
Test test = new Test();
test.show(n);
}

}

评分

参与人数 1黑马币 +2 收起 理由
yefeidd + 2 思路新颖,给两个币奖励下

查看全部评分

回复 使用道具 举报
饰演自己 发表于 2015-9-26 19:23
import java.util.Scanner;

public class Test {

思路很新颖,但是代码需要改进..
回复 使用道具 举报
import java.io.*;
public class libx {
  static void print(int n,char c)
{
     int i ;
     for(i=0;i<n;i++)
     {
         System.out.print(c);
     }
}
public static void main(String args[])
{
    try
  {
   BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
   System.out.print("请输入正六边形的边长:");
   int n=Integer.parseInt(br.readLine());
    int i;
       int j=n;
       for(i=n-1;i>=0;i--)
       {
           print(i,' ');
           if(i==n-1)
           {
               print(n,'*');
               System.out.println();
           }
           else
           {
            System.out.print("*");
               print(j,' ');
               j=j+2;
               System.out.print("*\n");
           }
       }
       j=j-4;
       for(i=1;i<n;i++)
       {
           print(i,' ');
           if(i==n-1)
           {
               print(n,'*');
               System.out.print("\n");
           }
           else
           {
            System.out.print("*");
               print(j,' ');
               j=j-2;
               System.out.print("*\n");
           }
       }   
   
  }
  catch(IOException e)
  {
   System.err.println(e.toString());
  }
   }}
回复 使用道具 举报
ll5353231 发表于 2015-9-26 19:42
import java.io.*;
public class libx {
  static void print(int n,char c)

兄弟,你这我看不懂啊。能不能来个简单的
回复 使用道具 举报
                public static void print(int m) {
                          
                                for (int x=1;x<=m ; x++) {
                                        if (x==1 ) {  
                                                for (int y=1;y<m ;y++ ) {
                                                        System.out.print(" ");
                                                }
                                                        System.out.print("*");
                                        }

                                        if (x>1&&x<m) {
                                                for (int y=1;y<=m+x-1 ;y++) {
                                                        if (y==m-x+1 || y==m+x-1) {
                                                                        System.out.print("*");
                                                        }else System.out.print(" ");
                                                }
                                        }
                                        if (x==m ) {
                                                System.out.print("*");
                                                for (int y=1;y<m ;y++ ) {
                                                        System.out.print(" *");
                                                }
                                        }
                                        System.out.println();

                                }               
                        }

评分

参与人数 1黑马币 +10 收起 理由
yefeidd + 10 很给力&lt;span id=&quot;transmark&quot;&gt;

查看全部评分

回复 使用道具 举报
发晚了,这是我的思路一
  1. package test;
  2. /*
  3.     *   
  4.    * *   
  5.   *   *  
  6. *     *
  7. * * * * *

  8. 分析图形的规律---每行有 2n-1 个字符
  9. 考虑一:
  10. 行1: 4 空格     1星+0个空格                        4空格
  11. 行2: 3 空格     1星+1空格+1空格+1星            3空格   
  12. 行3: 2 空格     1星+2空格+2空格+1星            2空格
  13. 行4: 1 空格     1星+3空格+3空格+1星            1空格
  14. 行5: 0 空格     1星+4空格+4空格+1星            0空格
  15. 二维字符数组 char[n][] chs = new char[]{' ',' ',' ',' ',' ',' ',' ',' ',' '}
  16. chs[0] = {' ',' ',' ',' ','*',' ',' ',' ',' '}   (2n-1)/2- 0
  17. chs[1] = {' ',' ',' ','*',' ','*',' ',' ',' '}   (2n-1)/2- 1   (2n-1)/2 +1
  18. chs[2] = {' ',' ','*',' ',' ',' ','*',' ',' '}   (2n-1)/2- 2   (2n-1)/2 +2
  19. chs[3] = {' ','*',' ',' ',' ',' ',' ','*',' '}   (2n-1)/2- 3   (2n-1)/2 +3
  20. chs[4] = {'*',' ','*',' ','*',' ','*',' ','*'}   (2n-1)/2- 4   (2n-1)/2 +4

  21. */
  22. public class HollowTriangle {

  23.         public static void printHollow(int n)
  24.         {
  25.             int num = 2*n-1; //共2n-1个字符
  26.                 char[][] chs = new char[n][num]; //
  27.        
  28.                 //初始化二维数组
  29.                 //以第一行标准格式,初始化所有的数组
  30.                 for(int i = 0; i<n; i++)
  31.                 {
  32.                         for(int j = 0; j<num; j++)
  33.                         {
  34.                                 if(j == n-1)
  35.                                 {
  36.                                         chs[i][j] = '*';
  37.                                 }
  38.                                 else {
  39.                                         chs[i][j] = ' ';
  40.                                 }
  41.                         }
  42.                 }
  43.                 //替换空格
  44.                 for(int i=1; i<n; i++)
  45.                 {
  46.                         for(int j=0; j<num; j++)
  47.                         {
  48.                                 //start:n-1-i, 对称点: n-1  end: (2n-1-1)-start= n+i-1
  49.                                 if(j == n-1-i || j== n+i-1)
  50.                                 {
  51.                                         chs[i][j] = '*';
  52.                                 }
  53.                                 else {
  54.                                         chs[i][j] = ' ';
  55.                                 }
  56.                                 if(i == n-1)
  57.                                 {
  58.                                         if(j%2 == 0)
  59.                                                 chs[i][j] = '*';
  60.                                 }
  61.                         }
  62.                 }
  63.                 //打印二维数组
  64.                 for(int i=0; i<n; i++)
  65.                 {
  66.                         for(int j=0; j<num; j++)
  67.                         {
  68.                                 System.out.print(chs[i][j]);
  69.                         }
  70.                         System.out.println();
  71.                 }       
  72.                 }
  73.         public static void main(String[] args) {
  74.                 printHollow(7);
  75.         }

  76. }
复制代码
回复 使用道具 举报
这里打印了 长度为7的镂空三角形,长度为6的也可以打印
回复 使用道具 举报
严旭晟 发表于 2015-9-26 22:44
发晚了,这是我的思路一

恩,我用杨辉三角的模式做出来了
  1. class   triangleDemo {
  2.         public static void main(String[] args) {
  3.                 int n = 10;
  4.                 char [][] arr = new char[n][2*n-1];
  5.                 arr[0][n-1] = '*';
  6.                 for(int i = 0; i < n; i++) {
  7.                         for(int j = 0; j < 2*n - 1; j++) {
  8.                                 if(arr[i][j] == '*'||arr[i][j] == '#') {
  9.                                         if(i < n-2) {
  10.                                                 if(arr[i+1][j-1]!='*')
  11.                                                 arr[i+1][j-1] = '*';
  12.                                                 else  arr[i+1][j-1] = '#';
  13.                                                 if(arr[i+1][j+1]!='*')
  14.                                                 arr[i+1][j+1] = '*';
  15.                                                 else  arr[i+1][j+1] = '#';       
  16.                                         }
  17.                                         else if(i == (n-2)) {
  18.                                                 arr[i+1][j-1] = '*';
  19.                                                 arr[i+1][j+1] = '*';
  20.                                         }
  21.                                 }
  22.                                 else
  23.                                         arr[i][j] = ' ';
  24.                                 if(arr[i][j] == '*'){
  25.                                         System.out.print(arr[i][j]);
  26.                                 }
  27.                                 else System.out.print(' ');
  28.                         }
  29.                         System.out.println();
  30.                 }


  31.         }
  32. }
复制代码
回复 使用道具 举报
严旭晟 发表于 2015-9-26 22:44
发晚了,这是我的思路一

因为没有多少币了,所以就不给了哈{:2_36:}
回复 使用道具 举报
hepann 发表于 2015-9-26 21:38
public static void print(int m) {
                          
                                for (int x=1;x

谢谢楼主 有机会见个面啊
回复 使用道具 举报
class Test7
{
        public static void main(String[] args)
        {
                print(10);
        }
        public static void print (int a)
        {
                //外循环,控制行数
                for (int i = 1;i <= a ;i++ )
                {
                        //打印每行前面的空格
                        for (int j = a - i;j >= 0 ;j-- )
                        {
                                System.out.print(" ");
                        }
                       
                        //打印每行第一个 *
                        System.out.print("*");

                        //如果只有一行,则跳出循环
                        if (i == 1)
                        {
                                System.out.println();
                                continue;
                        }

                        //如果在中间的行
                        else if (i >1 && i < a)
                        {
                                //打印每行中间空格
                                for (int y = 0;y < (2 *(i - 1) - 1);y++ )
                                {
                                        System.out.print(" ");
                                }

                                //打印每行最后一个 *
                                System.out.print("*");
                        }

                        //最后一行
                        else
                        {
                                //打印一个空格和一个* 的循环
                                for (int z = 2;z <= a ;z++ )
                                {
                                        System.out.print(" " + "*");
                                }
                        }
                               
                        System.out.println();
                }
                System.out.println();
        }
}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马