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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 也许_还不懂 中级黑马   /  2015-4-9 19:39  /  4178 人查看  /  20 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

10黑马币
一个for嵌套循环的练习
打印一个菱形吧

最佳答案

查看完整内容

class lingxingDemo { public static void main(String[] args) { for(int x=0;x

20 个回复

倒序浏览
class  lingxingDemo
{
        public static void main(String[] args)
        {
                for(int x=0;x<7;x++){
                        for(int y=x+1;y<7;y++){
                                System.out.print(" ");
                        }
                        for(int z=0;z<=x;z++){
                                System.out.print("* ");
                        }
                        System.out.println();               
                }
                for(int x=0;x<6;x++){
                        for(int y=0;y<=x;y++){
                                System.out.print(" ");
                        }
                        for(int z=x;z<6;z++){
                                System.out.print("* ");
                        }
                        System.out.println();               
                }
        }
}
回复 使用道具 举报
挺快的,真棒 给你了
回复 使用道具 举报
今天恰好做了一个,贴在这里,希望你能看明白啊,说实话套路有点傻逼!;P
  1. #include <stdio.h>
  2. #include <math.h>
  3. //我要用abs函数;
  4. int main(int argc, const char * argv[]) {
  5.     int row = 0;
  6.     printf("请输入想要打印的行数:");
  7.     // 由于输出的时菱形,所以行数恰好等于最多行的*个数;
  8.     scanf("%d",&row);
  9.    
  10.    
  11.     for(int i = 1;i<=row;i++)          // 外循环,确定当前打印第几行
  12.     {
  13.         for(int j=0;j<abs((row+1)/2,i);j++)   // 这个循环用于打印前面的空白; 当前行空白的个数与当前行的关系是abs((row+1)/2,i)
  14.         {
  15.             printf(" ");
  16.         }
  17.         int starCount =((row+1)/2-abs((row+1)/2,i))*2-1;  // 当前行*的个数与行的关系是((row+1)/2-abs((row+1)/2,i))*2-1
  18.         for(int m = 0;m <starCount;m++)       // 这个循环用于打印后面的*;
  19.         {
  20.             printf("*");
  21.         }
  22.         printf("\n");
  23.     }
  24.     return 0;
  25.    
  26. }
复制代码
回复 使用道具 举报
两个三角形分开打印
回复 使用道具 举报
綦敦涛 发表于 2015-4-9 20:10
今天恰好做了一个,贴在这里,希望你能看明白啊,说实话套路有点傻逼!

哎呀 熟人耶  赞一个
回复 使用道具 举报
是么?我得ID是不是太实在了……:lol
回复 使用道具 举报
261406938 发表于 2015-4-12 22:31
哎呀 熟人耶  赞一个

是么?我的ID是不是太实在了?!
回复 使用道具 举报
綦敦涛 发表于 2015-4-12 22:48
是么?我的ID是不是太实在了?!

哈哈哈  你猜我是谁;P
回复 使用道具 举报
是的啊。。。
回复 使用道具 举报
261406938 发表于 2015-4-12 23:00
哈哈哈  你猜我是谁

头像是你么?认不出来
回复 使用道具 举报
綦敦涛 发表于 2015-4-13 22:51
头像是你么?认不出来

当然是啊
回复 使用道具 举报

那是谁啊?
回复 使用道具 举报

是我啊:L
回复 使用道具 举报
eeeeeeeeeeeeeeeeeeee
回复 使用道具 举报
路过。。。。。。。
回复 使用道具 举报
synhm 中级黑马 2015-4-16 19:51:18
17#
路过学习。。。。。
回复 使用道具 举报
学学~~~~~~~~~~~~~~~~~~~~
回复 使用道具 举报
这个比较简单,可以看看基础班的视频就会了
回复 使用道具 举报
package cn.itcast;

import java.util.Scanner;

public class PrintRanc {
        public static void main(String[] args) {
                System.out.println("请输入要菱形的高度");
                Scanner sc = new Scanner(System.in);
                Integer high = sc.nextInt();
                int i = 0;
                // 先打印上三角,控制上上角的函数;
                for (i = 0; i < high - 1; i++) {
                        System.out.println();
                        // 打印每行*之前的空格数
                        for (int j = 20 - 2 * i; j >= 0; j--) {
                                System.out.print(" ");
                        }
                        // 控制*号个数
                        for (int t = 2 * i + 1; t > 0; t--) {
                                System.out.print("* ");
                        }
                }
                // 打印下三角
                for (i = high; i >= 0; i--) {
                        System.out.println();
                        // 打印每行*之前的空格数
                        for (int j = 20 - 2 * i; j >= 0; j--) {
                                System.out.print(" ");
                        }
                        // 控制*号个数
                        for (int t = 2 * i + 1; t > 0; t--) {
                                System.out.print("* ");
                        }
                }

        }
}



                     *
                   * * *
                 * * * * *
               * * * * * * *
           * * * * * * * * * * *
             * * * * * * * * *
               * * * * * * *
                 * * * * *
                   * * *
                     *
回复 使用道具 举报
12下一页
您需要登录后才可以回帖 登录 | 加入黑马