How do you convert a number value into that amount of characters? [duplicate]

I want to convert a number (20) into vertical lines and have those lines display a character (*) increasing by one as the lines increase.

I have:

import java.util.*;

public class increase {

    public static void main(String[]args) {
        Scanner input = new Scanner (System.in);
        
        System.out.println("Please enter the rumber of rows.");
        int tot = input.nextInt();
        
        for(int rows=0; rows<=tot; rows++) {
            System.out.println((rows to characters here));
        }
    }
}

  • Can you, please, give an example of what you want? For an example, the input does not necessarily have to be 20.

    – 

  • 1

    something like "*".repeat(count) ? But not clear what is meant by “rows to characters“. These whole question seem like one of the many draw a triangle pattern assignments

    – 




  • Try the following, for (int i = 1; i <= 20; i++) System.out.println(“*”.repeat(i));.

    – 

  • The question might not be a duplicate of the question linked in the closed notice. But, this question is lacking in details, so could be closed for that reason.

    – 

You can achieve this by modifying your for loop to control the number of characters printed on each line:

import java.util.*;

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

        Scanner input = new Scanner(System.in);

        System.out.println("Please enter the number of rows.");
        int tot = input.nextInt();

        for (int rows = 1; rows <= tot; rows++) {
            // Print '*' characters based on the current row number
            for (int i = 1; i <= rows; i++) {
                System.out.print("*");
            }
            // Move to the next line after printing the characters
            System.out.println();
        }
    }
}

Leave a Comment