How to make a diamond using Nested For Loops

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

    This should work. You probably only need most of the methods and printDiamond(_);

        import java.util.Scanner;
    public class StarsTry 
    {
    
        public static void main(String[] args) 
        {
            int reader;
            Scanner kBoard = new Scanner(System.in);
            do
            {
                System.out.println("Insert a number of rows: ");
    
                    reader = kBoard.nextInt();
                    printDiamond(reader); 
    
    
            }while(reader != 0);
    
        }
    
        public static void printStars(int n)
        {
            if(n >= 1)
            {
                System.out.print("*");
                printStars(n - 1);
            }
        }
    
        public static void printTopTriangle(int rows)
        {
            int x = 1;
            for(int j = (rows - 1); j >= 0; j--,x +=2)
            {
                printSpaces(j);
                printStars(x);
                System.out.print("\n");
            }
        }
    
        public static void printSpaces(int n)
        {
            if(n >= 1)
            {
                System.out.print(" ");
                printSpaces(n - 1);
            }
        }
    
        public static void printBottomTriangle(int rows, int startSpaces)
        {
            int x = 1 + (2*(rows - 1));
            for(int j = startSpaces; j <= (rows) && x > 0; j++,x -=2)
            {
                printSpaces(j);
                printStars(x);
                System.out.print("\n");
            }
        }
        public static void printBottomTriangle(int rows)
        {
            int x = 1 + (2*(rows - 1));
            for(int j = 0; j <= (rows - 1) && x > 0; j++,x -=2)
            {
                printSpaces(j);
                printStars(x);
                System.out.print("\n");
            }
        }
    
        public static void printDiamond(int rows)
        {
            printTopTriangle((int)rows/2 + 1);
            printBottomTriangle((int)rows/2, 1);
        }
    }
    
    0 讨论(0)
  • 2021-01-01 07:53
    class Inc_Dec
    {
        public static void main(String[] args)
        {
           int le=11;int c=0;int j1=(le/2)+1;int j2=le-j1;
           for(int i=1;i<=le;i++)
           {
               if(c<j1)
               { 
                 for(int k=(j1-i);k>0;k--)
                 {
                  System.out.print(" ");
                 }  
                 for(int j=1;j<=i;j++)
                 {
                      System.out.print("*"+" ");
                 }
                 c++;
                 System.out.println();
               }
               else
               {
                   for(int k=(i-j1);k>0;k--)
                   {
                       System.out.print(" ");
                   }  
                   for(int j=(le-i+1);j>0;j--)
                   {
                       System.out.print("*"+" ");
                   }
                   System.out.println();
               }
           }
        }
    }
    
    0 讨论(0)
  • 2021-01-01 07:54
    import java.util.Scanner;
    
    
    public class Diamond {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner in=new Scanner(System.in);
            int input=in.nextInt();
            int min=1;
            for(int i=0;i<input;i++){
                for(int j=input-1;j>i;j--){
                    System.out.print(" ");
                }
                for(int k=0;k<min;k++){
                    if(k%2==0){
                        System.out.print("*");
                    }else{
                        System.out.print(".");
                    }
    
                }
                min+=2;
                System.out.println();
            }
            int z=input+input-3;
            for(int i=1;i<input;i++){
                for(int j=0;j<i;j++){
                    System.out.print(" ");
                }
                for(int k=0;k<z;k++){
                    if(k%2==0){
                        System.out.print("*");
                    }else{
                        System.out.print(".");
                    }
                }
                z-=2;
                System.out.println();
    
            }
    
        }
    
    }
    
    0 讨论(0)
  • 2021-01-01 07:56

    I have the exact classwork in my university which also requires me to finish it in 3 for loops.

    And this is how I did it.

    In a simple way of explanation, I divide the diamond into two parts.

    no. of lines no. of spaces no. of stars total no. of slots
    1 4 1 5
    2 3 3 6
    3 2 5 7
    4 1 7 8
    5 0 9 9
    6 1 7 8
    7 2 5 7
    8 3 3 6
    9 4 1 5

    I want to find the no. of slots and the no. of spaces with each line, then allocating the no. of stars would be really easy.

    And considering the no. of slots, line 1 - 5 and line 6 - 9 would become two separate groups(i.e. middleLine).

    The equation of the no. of slots of the first half would be numberOfLines(i.e. i) + (middleLine - 1) where middleLine - 1 would be 4 when the maxNumberOfLines is 9.

    The equation of the no. of slots of the last half would be middleLine(i.e. replacement of I) + (middleLine - 1)(i.e. same as above) - (i - middleLine) where i - middleLine would be -1 when i = 6.

    And for the space, the first half would be middleLine - i and last half would be i - middleLine, which are exactly in a negative relationship(or symmetrical regarding their slopes).

    public class printOutDiamondWith3Loops {
    
    public static void main(String[] args) {
        
        int userInput = 9;
    
        double maxNumberOfLines = userInput;
        // double type is used instead of integer type in order to prevent removal of remainder when a division performed.
        double middleLine = Math.ceil(maxNumberOfLines/2);
    
        // Print out the diamond.
        for (int i = 1; i <= maxNumberOfLines; i++) { 
        // Determine the number of lines, which is also the maximum number of slots (the line in the middle).
            if (i <= middleLine) { 
            // Separate the whole diamond into two parts(as mentioned above).
                for (int j = 1; j <= i + ((middleLine - 1)); j++) {
                // Determine the no. of slots in each line from line 1 to 5.
                    if (j <= middleLine - i) { 
                    // Determine the no. of spaces and stars.
                        System.out.print(" ");
                    } else {
                        System.out.print("*");
                    }
                }
            } else { // i > middleLine
                for (int k = 1; k <= (middleLine + (middleLine - 1)) - (i - middleLine); k++) {
                // Determine the no. of slots in each line from line 6 to 9.
                // For better understanding, I did not simplify the above condition.
                // Noticeably, the first middleLine in above for loop is a replacement of i.
                    if (k <= i - middleLine) {
                    // Determine the no. of spaces and stars.
                        System.out.print(" ");
                    } else {
                        System.out.print("*");
                    }
                }
            } 
            System.out.println();   
        }
    
    }
    

    With such a frame, it is much easier to make further changes, such as letting the user input the no. of lines they want.

    Hope this answer could help you.

    I could lend you a more detailed version of my work, though not necessarily in need(the above explanation already explains the concepts): print-Out-Diamond-With-3-Loops-Advanced-Version.java

    0 讨论(0)
  • 2021-01-01 07:57
        for (int i = 0; i < 5; i++) 
              System.out.println("    *********".substring(i, 5 + 2*i));
    
        for (int i =5; i>0;i--)
            System.out.println("     **********".substring(i-1,5+(2*i)-3));
    
    0 讨论(0)
  • 2021-01-01 07:58

    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

    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

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

    9/2 + 1 = 5.5

    which will be taken as 5 in int.

    so first you will make rows.. 9 rows hence

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

    then

    print spaces like i did

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

    then finally stars

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

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

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