我被要求提高空间效率,我假设我需要使用循环,但我不完全确定如何使用,将不胜感激。
(作为参考,这段代码的目的是使用已经在另一个程序中收集的数据将图形显示到另一个程序中)
public class StudentChart
{
public StudentChart(int[] moduleMarks) //Constructor
{
Bar y = new Bar();
y.makeVisible();
y.changeSize(1, 100);
y.moveVertical(100);
y.moveHorizontal(-1);
y.changeColour(Colour.BLACK);
//y-axis is produced
Bar x = new Bar();
x.makeVisible();
x.changeSize(200,1);
x.moveVertical(200);
x.changeColour(Colour.BLACK);
//x-axis is produced
draw(moduleMarks);
printSummary(moduleMarks);
}
public static void draw(int[] moduleMarks)
{
int a = moduleMarks[0];
int b = moduleMarks[1];
int c = moduleMarks[2];
int d = moduleMarks[3];
int e = moduleMarks[4];
int f = moduleMarks[5];
//stores module marks from array as variables to be used later
Bar mod1 = new Bar();
Bar mod2 = new Bar();
Bar mod3 = new Bar();
Bar mod4 = new Bar();
Bar mod5 = new Bar();
Bar mod6 = new Bar();
mod1.makeVisible();
mod2.makeVisible();
mod3.makeVisible();
mod4.makeVisible();
mod5.makeVisible();
mod6.makeVisible();
//Bars are initialised and made visible
mod1.moveVertical(200-a);
mod2.moveVertical(200-b);
mod3.moveVertical(200-c);
mod4.moveVertical(200-d);
mod5.moveVertical(200-e);
mod6.moveVertical(200-f);
//Bars are moved based on their height so that they touch the x-axis
mod1.changeSize(15, a);
mod2.changeSize(15, b);
mod3.changeSize(15, c);
mod4.changeSize(15, d);
mod5.changeSize(15, e);
mod6.changeSize(15, f);
//Bar height changes depending on the module marks
mod1.moveHorizontal(0);
mod2.moveHorizontal(35);
mod3.moveHorizontal(70);
mod4.moveHorizontal(105);
mod5.moveHorizontal(140);
mod6.moveHorizontal(175);
//Bars are moved across so can be seen on chart
if (a<35)
{
mod1.changeColour(Colour.RED);
}
if (a>= 35 && a<40)
{
mod1.changeColour(Colour.YELLOW);
}
if (a>= 40 && a<70)
{
mod1.changeColour(Colour.GREEN);
}
if (a>= 70)
{
mod1.changeColour(Colour.MAGENTA);
}
if (b<35)
{
mod2.changeColour(Colour.RED);
}
if (b>= 35 && a<40)
{
mod2.changeColour(Colour.YELLOW);
}
if (b>= 40 && a<70)
{
mod2.changeColour(Colour.GREEN);
}
if (b>= 70)
{
mod2.changeColour(Colour.MAGENTA);
}
if (c<35)
{
mod3.changeColour(Colour.RED);
}
if (c>= 35 && a<40)
{
mod3.changeColour(Colour.YELLOW);
}
if (c>= 40 && a<70)
{
mod3.changeColour(Colour.GREEN);
}
if (c>= 70)
{
mod3.changeColour(Colour.MAGENTA);
}
if (d<35)
{
mod4.changeColour(Colour.RED);
}
if (d>= 35 && a<40)
{
mod4.changeColour(Colour.YELLOW);
}
if (d>= 40 && a<70)
{
mod4.changeColour(Colour.GREEN);
}
if (d>= 70)
{
mod4.changeColour(Colour.MAGENTA);
}
if (e<35)
{
mod5.changeColour(Colour.RED);
}
if (e>= 35 && a<40)
{
mod5.changeColour(Colour.YELLOW);
}
if (e>= 40 && a<70)
{
mod5.changeColour(Colour.GREEN);
}
if (e>= 70)
{
mod5.changeColour(Colour.MAGENTA);
}
if (f<35)
{
mod6.changeColour(Colour.RED);
}
if (f>= 35 && a<40)
{
mod6.changeColour(Colour.YELLOW);
}
if (f>= 40 && a<70)
{
mod6.changeColour(Colour.GREEN);
}
if (f>= 70)
{
mod6.changeColour(Colour.MAGENTA);
}
//Colour changes depending on module mark
//Could be improved
}
public static void printSummary(int[] moduleMarks)
{
for(int i =0; i<moduleMarks.length; i=i+1)
{
System.out.println("Module "+ (i+1) + " " + moduleMarks[i]);
}
//Prints module marks in a table
}
public static void main(String[] args)
{
}
}