Results 1 to 3 of 3
  1. #1
    miraclefruit is offline Member
    Join Date
    Nov 2016
    Posts
    10
    Rep Power
    0

    Question Bar chart printing program

    One interesting application of computers is to display graphs and bar charts. Write an application that reads five numbers between 1 and 30. For each number thatís read, your program should display the same number of adjacent asterisks. For example, if your program reads the number 7, it should display *******. Display the bars of asterisks after you read all five numbers.

    This is what I have so far, and I have attached the output. My output looks pretty similar to the expected result but it's still a little off. If anyone knows how to fix this please let me know.

    Java Code:
    import java.util.Scanner;
    public class BarChart
    {
     public static void main(String args[])
    
       {
          int i, j, number;
    	Scanner input = new Scanner(System.in);
     
          
          System.out.printf("Enter an integer(1-30): ");
         
         for (i = 0; i < 5; i++)
         {
            number = input.nextInt();
    		 if(number > 30 || number < 1) 
    		 {
    			 System.out.print("Out of Range Re-Enter");
    			 number = input.nextInt();
    		 }
    		 
     
            for (j = 1; j < number; j++) 
            {
                System.out.printf("*");
             }
      
            System.out.println();
    	 }
     }
    }
    Thank you!
    Attached Thumbnails Attached Thumbnails Bar chart printing program-snip.jpg  

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Bar chart printing program

    it's still a little off.
    Can you describe what the problem is and what should be changed to make it right?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Bar chart printing program

    For any given number, how many times to you think the following loop statement will execute?

    Java Code:
    for (j = 1; j < number; j++)
    When you figure that out, you have solved the problem.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 0
    Last Post: 04-26-2013, 03:48 PM
  2. jfreechart plotting line chart on candlestick chart
    By amlwwalker in forum New To Java
    Replies: 0
    Last Post: 10-07-2012, 02:18 AM
  3. Part of program not printing ?
    By graz in forum New To Java
    Replies: 2
    Last Post: 04-09-2009, 04:37 AM
  4. Jsp chart program
    By sundarjothi in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 06-30-2008, 04:00 PM
  5. A Fibonacci printing program
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-28-2008, 08:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •