How to make a diamond using Nested For Loops

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

    I can see what you are trying to do and this is a pretty neat way to think about the diamond.

    You will have some issues with the j counter when i goes negative..look at how to use Math.abs()

    Also try writing some pseudo code in basic steps with comments to get the pattern clear:

     //print 5 spaces + 1 star
     //print 4 spaces + 2 stars
     //print 3 spaces + 3 stars
     //print 2 spaces+ 4 stars
     .
     . 
     .  
     //print 5 spaces + 1 star
    

    Then, literally substitute variables (j and i) for the numbers.

    You now have a model. This is often the hardest part in programming..getting the model right. Only jump into coding when you have a good idea for how the model works.

    Once you have the variables substituted, you can try to convert the whole thing into an automated loop.

    0 讨论(0)
  • 2021-01-01 07:38

    Try this

    public class Main
    {
        public static void main(String[] args) {
    
            int n = 50;
            int space = n - 1;
    
            for(int i = 0; i < n; i++){
    
                for(int j = 0; j < space; j++) {
                    System.out.print(" ");
                }
    
                for(int j = 0; j <= i; j++) {
                     System.out.print("* ");
                }
    
                System.out.println("");
                space--;
            }
    
            space = 0;
    
            for(int i = n; i > 0; i--){
    
                for(int j = 0; j < space; j++){
    
                    System.out.print(" ");
                }
    
                for(int j = 0; j < i; j++) {
                    System.out.print("* ");
                }
    
                System.out.println("");
                space++;
            }
    
        }
    }
    
    0 讨论(0)
  • 2021-01-01 07:40
    public class Diamond {
    
    //Size of the diamond
    private int diagonal;
    
    public Diamond(int diagonal) {
        this.diagonal = diagonal;
    }
    
    public void drawDiamond() {
        int n = diagonal;
        for (int i = n / 2; i >= -n / 2; i--) {
            for (int k = 0; k < i; k++) {
                System.out.print(" ");
            }
            for (int j = 1; j <= (n - i * 2) && i >= 0; j++) {
                System.out.print("*");
            }
            for (int k = 1; k <= -i && i < 0; k++) {
                System.out.print(" ");
            }
            for (int j = (n / 2) * 2 + 2 * i; j >= -(n % 2 - 1) && i < 0; j--) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
    
    public static void main(String[] args) {
        Diamond a = new Diamond(21);  //You pass diamond size here in the constructor
        a.drawDiamond();
    }
    }
    

    The main problem is parity of diagonal. If it's even you can't properly draw top asterisk. So there is 2 types of diamonds - with even and odd diagonal (with 2 and 1 asterisk at the top).

    0 讨论(0)
  • 2021-01-01 07:40
    package com.DiamondPrintingProgram;
    
    import java.util.Scanner;
    
    public class DiamondPrintingProgram {
    
        public static void main(String[] args) { 
            int num = getInput();
            int middle = (int) num / 2 + 1;
            printOutput(num,middle); 
        }
        public static int getInput() {
            Scanner sc = new Scanner(System.in);
            int num;
            System.out.print("Enter a odd number: ");
            while (true) {
                num = sc.nextInt();
                if (num % 2 != 0) {
                    break;
                }
                System.out.print("Please Enter a ODD NUMBER: ");
            }
            return num;
        }
        private static void printOutput(int num, int middle){
            char asterisk = '*';
            for (int j = 0; j < num; j++) {
                for (int i = 1; i <= num; i++) {
                    if (j < middle) {
                        if ((i < (middle - j) || i > (middle + j))) {
                            System.out.print(' ');
                        } else {
                            System.out.print(asterisk);
                        }
                    } else {
                        if ((i < (j - middle + 2)) || (i > (2 * num - j - middle))) {
                            System.out.print(' ');
                        } else {
                            System.out.print(asterisk);
                        }
                    }
                }
                System.out.println();
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-01 07:41
    import static java.lang.System.out;
    import java.util.Scanner;
    public class Diamond {
    
        public static void main(String[] args) {
    
        Scanner sc=new Scanner(System.in);
        int row=sc.nextInt();
        sc.close();
        Diamond d=new Diamond();
        d.upperDiamond(row);
        d.lowerDiamond(row-2);
    
        }
        public void upperDiamond(int a){
    
        for(int i=0;i<a;i++){
            for(int j=a-1;j>i;j--)
                out.print(" ");
            for(int k=0;k<2*i-1;k++)
            out.print("*");
            out.print("\n");
            }
        }
        public void lowerDiamond(int b){
    
            for(int i=0;i<b;i++){
            for(int j=0;j<=i;j++)
            out.print(" ");
            for(int k=0;k<2*(b-i)-1;k++)
                out.print("*");
            out.print("\n");
            }
        }
    
    }
    
    0 讨论(0)
  • 2021-01-01 07:45
    public class MyDiamond
    {
       public static void main(String[] args)
       {
          int numRows=151;//Length of the pyramid that we want.151 is just an example
          int midrow = (numRows+1)/2;//midrow is the middle row and has numRows number of *
    
          int diff=0;
          for(int i=1;i<numRows+1;i++)
          {
             for(int j=1;j<numRows+1;j++)
             {
                if(((midrow-diff)<=j && (j<=midrow+diff)))
                {
                   System.out.print("*");
                }else
                {
                   System.out.print(" ");
                }
    
             }
             System.out.println();
             if(i<midrow)
             {
             diff++;
             }else
             {
                diff--;
             }
          }
    
    
    
    
       }
    
    }
    
    0 讨论(0)
提交回复
热议问题