samedi 22 juillet 2017

java nested loop patterns

I am having issues with nested loop to print a pattern. While most tutorials show a pyramid mine is more complex. any help would be welcome.

I need to print the following based on user unput for the # of rows. expl

2 rows

o //\\.
o//  \\.

4 rows

o      //\\.
o    //    \\.
o  //        \\.
o//            \\.

Code

import java.util.Scanner;
public class homework
{ //****CLASS***

public static void main (String[] args)
 {   //****MAIN***
  // int size = 2;
 int noOfRows = 3;

        //Scanner sc = new Scanner(System.in);


        System.out.println("How Many Rows You Want In Your Pyramid?");

       // int noOfRows = sc.nextInt();



        System.out.println("Here Is Your Pyramid");


        for (int i = 1; i <= noOfRows; i++)
        {
        System.out.print("o");
            //Printing i*2 spaces at the beginning of each row

            for (int j = noOfRows*2 + 1; j >= 1; j--)
            {
                System.out.print(" ");
            }

            //Printing j value where j value will be from 1 to rowCount

            for (int j = noOfRows + 1; j >= 1; j--)             
            {
                System.out.print("//");
            }

            //Printing j value where j value will be from rowCount-1 to 1

            for (int j = noOfRows + 2; j <= noOfRows; j++)
            {                 
                System.out.print("\\" + "\\" + ".");             
            }                          

                        System.out.println();

            //Incrementing the rowCount


        }      
System.out.println(); // NEWLINE
  //for out
 }//****MAIN***
}//****CLASS***  

Aucun commentaire:

Enregistrer un commentaire