mercredi 30 mars 2022

Triangle pattern in Java with while loop

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

Aucun commentaire:

Enregistrer un commentaire