Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class bars {
  3. public static void main(String[] args) {
  4. Scanner scan = new Scanner(System.in);
  5. //Ask user for information
  6. System.out.println("how many bars would you like to display? ");
  7. int totalbars = scan.nextInt();
  8. int[] bars = new int[totalbars];
  9. System.out.println("specify the sizes of the bars?");
  10. for(int i=0; i<totalbars; i++) {
  11. bars[i] = scan.nextInt();
  12. }
  13. scan.close();
  14.  
  15. int average = averagebar(bars);
  16. printbars("row", bars, average);
  17. }
  18. //Output bars, Print bars
  19. static void printbars(String row, int[] bars, int average) {
  20. for(int i=0; i < bars.length; i++) {
  21. // The variable "row" value is set to i+1 and printed out
  22. row = String.valueOf((i)+1);
  23. System.out.print( row + "");
  24. // The bar is then printed next to the variable "row"
  25. for(int j=0; j< bars[i]; j++) {
  26. System.out.println(" * ");
  27. }
  28. // Find the highest bar
  29. int highest=bars[0];
  30. for(int bar : bars){
  31. if(bar > highest)
  32. highest = bar;
  33. highest = 3 * highest + 2;
  34. }
  35. // Hyphen line is then printed out
  36. for(int a=0; a<highest; a++) {
  37. System.out.print("-" );
  38. System.out.println();
  39. //A is printed out, Average bar is printed out
  40. System.out.print("A ");
  41. for(int k=0; k < average; k++) {
  42. System.out.print("* " ); }
  43. System.out.println(); }
  44. }
  45. }
  46. static int averagebar (int[] bars) {
  47. double mean = 0;
  48. for(int b=0; b < bars.length; b++) {
  49. mean = mean+bars[b];
  50. mean /= bars.length;
  51. // Round value
  52. int averagebar = (int) Math.round(mean);
  53. return averagebar;
  54. }
  55. }
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement