Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class bars {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- //Ask user for information
- System.out.println("how many bars would you like to display? ");
- int totalbars = scan.nextInt();
- int[] bars = new int[totalbars];
- System.out.println("specify the sizes of the bars?");
- for(int i=0; i<totalbars; i++) {
- bars[i] = scan.nextInt();
- }
- scan.close();
- int average = averagebar(bars);
- printbars("row", bars, average);
- }
- //Output bars, Print bars
- static void printbars(String row, int[] bars, int average) {
- for(int i=0; i < bars.length; i++) {
- // The variable "row" value is set to i+1 and printed out
- row = String.valueOf((i)+1);
- System.out.print( row + "");
- // The bar is then printed next to the variable "row"
- for(int j=0; j< bars[i]; j++) {
- System.out.println(" * ");
- }
- // Find the highest bar
- int highest=bars[0];
- for(int bar : bars){
- if(bar > highest)
- highest = bar;
- highest = 3 * highest + 2;
- }
- // Hyphen line is then printed out
- for(int a=0; a<highest; a++) {
- System.out.print("-" );
- System.out.println();
- //A is printed out, Average bar is printed out
- System.out.print("A ");
- for(int k=0; k < average; k++) {
- System.out.print("* " ); }
- System.out.println(); }
- }
- }
- static int averagebar (int[] bars) {
- double mean = 0;
- for(int b=0; b < bars.length; b++) {
- mean = mean+bars[b];
- mean /= bars.length;
- // Round value
- int averagebar = (int) Math.round(mean);
- return averagebar;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement