Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Making a rain histogram

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Location
    in an extremely competitive and hopeless economy
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Making a rain histogram

    I prompted the user to enter inches of rainfall for seven days and stored them in an array. Now, I am trying to display a histogram of this data using a nested loop, but for each inch, I have to display a * like this:
    Rainfall Histogram:
    ****
    ***
    **
    ******
    ********
    **********
    **

    the only way I know how to display a number of *s according to user input is by an if statement. I copied and pasted from a similar class assignment I did, so what I have so far is pretty messy and nonsensical:



     
    static char DetermineRainfall(int inches) 
    {
     
    System.out.println("Histogram");
    	//char inches;
    	for(inches = 0; inches < 7; inches++) 
    	{
    		for(int j = 0; j < 1; j++) 
    			{ 
    				if (rainamount > .9 && rainamount <= 1)
    					inches = '*';
    					return inches;
    			}  
    }


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Making a rain histogram

    so what is your question?

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Location
    in an extremely competitive and hopeless economy
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Making a rain histogram

    how to convert the user's response (an integer) into an asterisk. for example, 3 would become ***

  4. #4
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Making a rain histogram

    oh, did you mean to return an asterisk whose length is equal to user's input (integer)?

    Here's how:
    create a method that has a return value of String
    create a local variable of String with initial value of ""
    create a loop that will fill that string with *

  5. The Following User Says Thank You to dicdic For This Useful Post:

    Wawlden (March 11th, 2014)

  6. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Making a rain histogram

    the only way I know how to display a number of *s according to user input is by an if statement
    That's an odd comment coming from someone who's been instructed to complete the assignment using nested for loops. Have you read the lesson on for loops, written any examples, and seen what can be done with them? Please show your actual code for this problem if you need additional help.

Similar Threads

  1. Histogram equalization Help Please!
    By dazzle1218 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 12th, 2013, 04:08 PM
  2. Array Histogram
    By Thor in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 4th, 2012, 08:06 AM
  3. [SOLVED] Printing A Histogram...
    By Nuggets in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 18th, 2012, 02:11 PM
  4. [SOLVED] BufferReader, histogram
    By Nhedro in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: February 22nd, 2012, 09:39 AM