Forums Register Login

Drawing in a loop

+Pie Number of slices to send: Send
Hi, this code should allow the value entered by the user to set the height of a bar. Then allow the user to enter 12 values, which it does, but only prints one bar. Does it need it's own loop, such as a while? If so where would i put this in? I can't think what the conditions would be, seeming they're allready set.

thanks,

import element.*;
import java.awt.Color;

/**
* Prompt user for 12 values of rainful
* and produce the heaviest, average value and bar chart.
* @Author Graham Robinson
* @version 1 November 2005
*/

public class AuchenWeather
{

public static void main (String[] args)
{

//consrtuct new console window
ConsoleWindow c = new ConsoleWindow();

// construct a drawing window object
DrawingWindow d = new DrawingWindow(370, 300);

//variable
int rain, heaviest = 0, entry = 0, x = 10, height;
final int y = 300, width = 20;
boolean valid;
float total = 0.0f, topEntry = 0.0f, average = 0.0f;


//for control
for(int count = 0; count < 12; count++)
{
do
{
c.out.println("Please input value of rainfull betweem 0 and 200mm.");
rain = c.input.readInt();//rainfall value entered by user
valid = (rain >= 0 && rain <= 200);
total += rain;//total of rainfall
entry++;//number of entries
x = x + 30;
height = rain;


d.move(x,y);// Draw rectangle
d.line(0, - height);
d.line(width,0);
d.line(0, height);
d.line(- width, 0);


if (!valid)
c.out.println("The value entered isn't valid.");
}while (!valid);


if (rain > heaviest)
heaviest = rain;
average = total / entry;




}//end of for

c.out.println("Heaviest rainfall = " + heaviest);
c.out.println("Average rainfall = " + average);



}//end of class
}//end of main
+Pie Number of slices to send: Send
Don't you think that you should only draw the rectangle when the variable 'valid' is true. In the code above, it'll increment x even when 'valid' is false (and I guess x stands for the value along x-axis).

The loops see to be alright but I did not understand the code written to draw the rectangle.

regards,
vijay.
We don't have time for this. We've gotta save the moon! Or check this out:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1438 times.
Similar Threads
Storing count within a while
Do whiles/Fors and Drawing
Create Bar Chart
double or float?
Passing parameters to functions help.
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 00:15:32.