-1

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

3 Answers 3

3

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(symbol.repeat(c));
    c++;
}

Example output

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

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
*
=====================
    *
   **
  ***
 ****
*****
2
  • Thanks but I want help with while loop
    – Nobody
    Mar 31, 2022 at 0:19
  • 1
    @Nobody have edited it and used while loop Mar 31, 2022 at 0:23
2

I cant edit the answer of @Elliott Frisch. Thats why I am giving this answer. You have to add only 3 lines extra to do what you want and there is no need to remove your Scanner object

First import java.io.*

import java.io.*;

In variables you have to add this line.

String str3 = " ";

And in while loop of logic section you have to replace this code .

System.out.println(str);

To this;

System.out.println(str3.repeat(inp)+str2);

And this is your whole code after editing.

import java.util.Scanner;
import java.io.*;

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///////////////////
 
  String str = str2;
  String str3 = " ";
  System.out.println("=====================");

  ///////LOGIC/////////////////////////////
while(inp != 0){
 
 System.out.println(str3.repeat(inp)+str2);
 str2 = str+str2; 
 inp=inp-1; 
}

/////////////////END///////////////////////////
 }
 
}

And this is the output

Please Enter how many rows do you want
10
Which symbol do you want
#
=====================
          #
         ##
        ###
       ####
      #####
     ######
    #######
   ########
  #########
 ##########

THANKS I HOPE IT WILL HELP YOU

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.