我试图让我的代码比较我随机生成的对象的高度,并按升序对它们进行排序。我目前在理解一些基本概念方面遇到了困难,而且尽管我阅读了java文档,但我找不到适合我的答案。我知道我的排序听众中没有任何东西,但我对从哪里去的地方感到有些不知所措。
编辑:我已经修复了部分代码,但现在问题在于它只重绘了一些条形图,有时它们会出现故障。
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class Lab7 extends JPanel
{
Random gen = new Random();
boolean tick = true;
private static bars[] bar = new bars[20];
private static Integer[] height = new Integer[20], heightTemp = new Integer[1];
private final int WIDTH = 500, HEIGHT = 300;
private final int DELAY = 900;
private int size = 15;
int[] y = new int[20], yTemp = new int[1];
int [] x = new int [20];
Timer timer;
public Lab7()
{
if(tick == true)
{
for(int count = 0; count < bar.length; count++)
{
height[count] = gen.nextInt(250) + 5;
y[count] = (HEIGHT - height[count]);
x[count] = (count * size +(count*5));
bar[count] = new bars(x[count], y[count], size, height[count]);
}
tick = false;
}
timer = new Timer(DELAY, new SortingListener());
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setBackground(Color.white);
timer.start();
}
public void paintComponent(Graphics page)
{
super.paintComponent(page);
for(int count = 0; count < bar.length; count++)
{
bar[count].draw(page);
}
}
private class SortingListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
int min = 0;
for(int count = 0; count < height.length; count++)
{
for(int index = count + 1; index < height.length; index ++)
{
if(height[count].compareTo(height[index]) > 0)
{
heightTemp[0] = height[count];
yTemp[0] = y[count];
height[count] = height[index];
y[count] = y[index];
height[index] = heightTemp[0];
y[index] = yTemp[0];
}
System.out.println(count + ":" + min);
if(index == 19 && height[count].compareTo(height[index]) != 0)
{
bar[count] = new bars(x[count], y[count], size, height[count]);
bar[index] = new bars(x[index], y[index], size, height[index]);
}
}
repaint();
}
timer.stop();
}
}
public static void main(String[]args)
{
JFrame frame = new JFrame("Lab7");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new Lab7());
frame.pack();
frame.setVisible(true);
}
}
这里也是Bars类。
public class bars{
Random gen = new Random();
private int width = 20, height, x, y;
private int R = gen.nextInt(256);
private int G = gen.nextInt(256);
private int B = gen.nextInt(256);
public Color RandomColor;
//creates a rectangle
public bars(int x, int y, int width, int height)
{
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public void draw(Graphics page)
{
Color RandomColor = new Color(R, G, B);
page.setColor(RandomColor);
page.fillRect(x, y, width, height);
Polygon p = new Polygon();
page.setColor(RandomColor);
page.fillPolygon(p);
}
public void setWidth(int width)
{this.width = width;}
public int getWidth()
{return width;}
public void setHeight(int height)
{this.height = height;}
public int getHeight()
{return height;}
public void setX(int x)
{this.x = x;}
public int getX()
{return x;}
public void setY(int y)
{this.y = y;}
public int getY()
{return y;}
public void setColor(Color RandomColor)
{this.RandomColor = RandomColor;}
public Color getColor()
{return RandomColor;}}
答案 0 :(得分:0)
如果您正在进行选择排序,则内部循环需要从外部循环的值开始。变化
for(int index = 0;
到
for(int index = count;
你得到的错误是列表没有排序吗?看起来只有“已排序”列表的第一个元素才会出现在正确的位置。