如何使用Nested For Loops制作钻石 [英] How to make a diamond using Nested For Loops

查看:134
本文介绍了如何使用Nested For Loops制作钻石的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我被指派用Java用星号制作一颗钻石,我真的很困惑.到目前为止,这是我要提出的:

So I was assigned to make a diamond with asterisks in Java and I'm really stumped. Here's what I've come up with so far:

public class Lab1 
{
   public static void main(String[] args)
   {
      for(int i = 5; i > -5; i--)
      {
         for(int j = 0; j < i; j++)
         {
            System.out.print(" ");
         }
         for(int j = 0; j >= i; j--)
         {
            System.out.print(" ");
         }
         System.out.println("*");
      }
   }
}

推荐答案

为了制作钻石,您需要设置形状的空间和星星.由于我是初学者,因此我仅使用嵌套循环制作了这个简单的程序.

in order to make a diamond you need to set spaces and stars in shape i have made this simple program using only nested loops since i am a beginner.

public class Diamond {
    public static void main(String[] args) {
        int size = 9,odd = 1, nos = size/2; // nos =number of spaces
        for (int i = 1; i <= size; i++) { // for number of rows i.e n rows
            for (int k = nos; k >= 1; k--) { // for number of spaces i.e
                                                // 3,2,1,0,1,2,3 and so on
                System.out.print(" ");
            }
            for (int j = 1; j <= odd; j++) { // for number of columns i.e
                                                // 1,3,5,7,5,3,1
                System.out.print("*");
            }
            System.out.println();
            if (i < size/2+1) {
                odd += 2; // columns increasing till center row 
                nos -= 1; // spaces decreasing till center row 
            } else {
                odd -= 2; // columns decreasing
                nos += 1; // spaces increasing

            }
        }
    }
}

如您所见,不,需要减少空间的数量,直到中心行和恒星的数量需要增加,但是在中心行之后,空间的数量相反,即空间增加而星的数量减少

as you can see nos that is number of spaces needs to be decreased till center row and number of stars needs to be increased but after center row its the opposite , i.e spaces increase and stars decrease

大小 可以是任何数字,我在此处将其设置为9,因此我将拥有9号星,最大9行9列...空格数(无)为

size can be any number i set it to 9 over here so i will have a size 9 star that is 9 rows and 9 columns max... number of space (nos) will be

9/2 = 4.5

9/2 = 4.5

但是java会将其设为4,因为int无法存储小数,并且中心行将为

but java will take it as 4 because int can not store decimal numbers and center row will be

9/2 +1 = 5.5

9/2 + 1 = 5.5

,它将被视为int中的5.

which will be taken as 5 in int.

所以首先您将创建行.因此有9行

so first you will make rows.. 9 rows hence

(int i = 1; i< = size; i ++)//size = 9

(int i=1;i<=size;i++)//size=9

然后

像我一样打印空间

(int k = nos; k> = 1; k--)//nos为size/2

(int k =nos; k>=1; k--) //nos being size/2

然后终于星星

(int j = 1; j< =奇数; j ++)

(int j=1; j<= odd;j++)

一旦行结束...您可以使用if条件调整星号和空格

once line ends... you can adjust stars and spaces using if condition

这篇关于如何使用Nested For Loops制作钻石的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆