[Solved] simple histogram
Hi , I would like to construct the simple histogram with symbols (e.g #or % or *).. based on the user integer input.. (input: 4 2 3), and i would like to display in this way.
+
+ +
+ + +
+ + + <---- histogram displayed with symbol
4 2 3 <---inputs (the number of input is fixed in this case)
Can you help me in how to display those symbols side by side.(in a histogram format), I am stuck when i tried to combine all the symbols together. My codes just display them in 1 vertical line instead of displaying on multiple vertical lines. below is my code. Thanks in advance.
Code:
import java.util.Scanner;
public class histogram {
public static void main(String[] args) {
int num1,num2,num3,num4;
Scanner sc = new Scanner (System.in);
System.out.print("Enter thhe numbers: ");
num1 = sc.nextInt();
String ss1 = genPattern(num1);
num2 = sc.nextInt();
String ss2 = genPattern(num2);
num3 = sc.nextInt();
String ss3 = genPattern(num3);
num4 = sc.nextInt();
String ss4 = genPattern(num4);
System.out.println(ss1 + ss2 + ss3 + ss4);
}
public static String genPattern(int number){
String msg= "";
String as = "+";
for (int i= 1; i<=number; i++){
msg=msg+ as + "\n" ;
}
return msg;
}
}