Home > front end >  Triangle pattern in Java with while loop
Triangle pattern in Java with while loop

Time:03-31

Hello I was Tasked to make an triangle generator with the symbols and number of rows that users want in java with while loop it worked like this.


*
**
***
****
*****
******
*******
********

But I want like this

       *
      **
     ***
    ****
   *****
  ******
 *******
********

Here is my code

import java.util.Scanner;
import java.lang.Math;

class Main {
 public static void main(String args[]){
  
  ////////////OBJECTS///////////////////////

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

  System.out.println("Please Enter how many rows do you want");
  int inp = sc.nextInt();
  
  System.out.println("Which symbol do you want");
  String str2 = sc2.nextLine();
 ////////////////VARIABLES///////////////////

  int c = 0; 
  String str = "";
  String str3 = "";
  System.out.println("=====================");

  ///////LOGIC/////////////////////////////

  while(c != inp){
   str = str str2; 
   System.out.println(str);
   c=c 1;
  }

////////////////////////////////////////////
  
 }
 
}

Do you know that how can i solve it with this logic

CodePudding user response:

You only need one Scanner, and the algorithm is much easier if you use String.repeat(int). First repeat space inp - c times, then repeat your "symbol" c times. Like,

Scanner sc = new Scanner(System.in);
System.out.println("Please Enter how many rows do you want");
int inp = sc.nextInt();
sc.nextLine();
System.out.println("Which symbol do you want");
String symbol = sc.nextLine(); // You had a good name in the prompt.
int c = 1;
System.out.println("=====================");
while (c <= inp) {
    System.out.print(" ".repeat(inp - c));
    System.out.println(str2.repeat(c));
    c  ;
}

Example output

Please Enter how many rows do you want
5
Which symbol do you want
*
=====================
    *
   **
  ***
 ****
*****

CodePudding user response:

For each row of the pattern, you can form a pattern between the whitespaces and the character that you are trying to print.

In case the input is 5, you can see that the whitespaces are decreasing (, first row 4, second row 3 ) and the character is increasing( first row 1 , second row 2 ) and the sum of number of whitespaces and the character(*) is constant( equal to 5) Therefore, for each row, you have to print whitespaces and character for a number of times.

Also, you don't need 2 scanner objects to read the inputs. After reading the integer, you can use scan.next() to read the last character(why?)

class Main {
    public static void main(String args[]) {

        //////////// OBJECTS///////////////////////

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

        System.out.println("Please Enter how many rows do you want");
        int inp = sc.nextInt();
        sc.nextLine();
        // sc.next();
        System.out.println("Which symbol do you want");
        String str2 = sc.nextLine();
        //////////////// VARIABLES///////////////////

        int c = 1;
        String str = "";
        String str3 = "";
        System.out.println("=====================");

        /////// LOGIC/////////////////////////////

         int i = 1;
    int j = 0;
    while (i <= inp) {
        while (j < inp - i) {
            System.out.print(" ");
            j  ;
        }
        while (j < inp) {
            System.out.print(str2);
            j  ;
        }
        System.out.println();
        j = 0;
        i  ;
    }

        sc.close();
        ////////////////////////////////////////////

    }

}

and the output is

Please Enter how many rows do you want
5
Which symbol do you want
*
=====================
    *
   **
  ***
 ****
*****
  • Related