How to make a diamond using Nested For Loops

后端 未结 15 1131
终归单人心
终归单人心 2021-01-01 07:09

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 s         


        
15条回答
  •  有刺的猬
    2021-01-01 07:59

    package practice;
    
    import java.util.Scanner;
    
    public class Practice {
    
    
        public static void main(String[] args) {
    
        for(int i=0;i<=10;i++)
        {
            if(i<=5)
            {
            for(int k=1;k<=5-i;k++)
            {
                System.out.print(" ");
            }
            for(int j=0;j<=i;j++)
            {
                System.out.print(" *");
            }
            }
            if(i>5)
            {
            for(int k=0;k<=i-6;k++)
            {
                System.out.print(" ");
            }
            for(int j=0;j<=10-i;j++)
            {
                System.out.print(" *");
            }
            }
            System.out.println();
        }
        }
    
    }
    

提交回复
热议问题