Java for loop print ASCII bar chart in console

Question

We would like to print out ASCII bar chart from console from user input.

If user input is 1,3,5,4,2, the output would be

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



import java.util.Scanner;

public class Main {

  public static void main(String[] args) {
    
    Scanner input = new Scanner(System.in);
    System.out.println("Enter 5 numbers beatween 1 and 30:");
    int number1 = input.nextInt();
    int number2 = input.nextInt();
    int number3 = input.nextInt();
    int number4 = input.nextInt();
    int number5 = input.nextInt();
    
    printBar(number1);
    printBar(number2);
    printBar(number3);
    printBar(number4);
    printBar(number5);
    
    input.close();
    
  }
  public static void printBar(int length){
    
    for(int i = 0; i < length; i++){
      System.out.print('*');
    }
    System.out.println();
  }
}



PreviousNext

Related