Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Drawing in a loop

Graham Robinson
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


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);

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++)
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(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
Vijay Raj
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!