How to make a diamond using Nested For Loops

后端 未结 15 1133
终归单人心
终归单人心 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

    **Note :- Using Count Global variable we can manage space as well as star increment and decrement**
    
    
    import java.util.*;
    public class ParamidExample
    {
        public static void main(String args[])
        {
            System.out.println("Enter a number");
            Scanner sc=new Scanner(System.in);
            int no=sc.nextInt();
    
            int count=1;
            for(int i=1;i<=2*no-1;i++)
            {
                for(int j=count;j<=no;j++)
                {
                    System.out.print("  "); 
                }
                for(int k=1;k<=count*2-1;k++)
                {
                    System.out.print("* ");
                }
            if(i<no)
                count++;
            else
                count--;
                    System.out.println(""); 
            }
        }
    }
    
    0 讨论(0)
  • 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();
        }
        }
    
    }
    
    0 讨论(0)
  • 2021-01-01 08:02
    import java.util.Scanner;
    
    public class MakeDiamond {
    public static void main(String[] args) {
    
        Scanner sc = new Scanner(System.in);
    
        while (true) {
    
            System.out.println("Let's Creat Diamonds");
            System.out.println("If number increases Diamonds gets bigger. Please input number lager than 1 : ");
    
            int user_input = sc.nextInt(); //gets user's input
            System.out.println("");
    
            int x = user_input;
            int front_space =  -5;
    
            for (int i = 0; i < 2 * user_input + 1; i++) {
                for (int a = front_space; a < Math.abs(i - user_input); a++) {
                    System.out.print("    "); //Change a bit if diamonds are not in good shape
                }
    
                if (i < user_input + 1) {
                    for (int b = 0; b < 2 * i + 1; b++) {
                        System.out.print("*  "); //Change a bit if diamonds are not in good shape
                    }
    
                } else if (i > user_input) {
                    for (int c = 0; c < 2 * x - 1; c++) {
                        System.out.print("*  "); //Change a bit if diamonds are not in good shape
                    }
                    x--;
                }
                System.out.print('\n');
            }
    
            System.out.println("\nRun Again? 1 = Run,  2 = Exit : ");
    
            int restart = sc.nextInt();
            System.out.println("");
    
            if (restart == 2) {
                System.out.println("Exit the Program.");
                System.exit(0);
                sc.close();
            }
        }
      }
    }
    

    When making diamonds with while or for loops. I think using 'Math.abs' will be the simplest way to make it.

    You can put number by Scanner, and when input number increases diamonds will get bigger.

    I used Eclipse to make this program. so, the Space will be differ by your running environment. like another IDE, CMD or Terminal. if diamonds are not in good shape. Just change spaces.

    0 讨论(0)
提交回复
热议问题