谁帮忙修改一下!!

cooma 2008-04-23 03:58:50
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
class Alg extends Applet{
static int set=1,delayTime=50;
static boolean stop=true;
Graphics mypen;
Button test=new Button("start");
int[] data={18,4,17,5,13,8,14,7,10,16,11,3,19,2,20,15,6,9,1,12};
public Alg(){
add(test);
setSize(400,530);
test.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getSource()==test) set++;
Choosealg();
}
});
}
public void paint(Graphics g){

}
public void Choosealg(){
switch(set){
case 1:
mypen.setColor(Color.black);
mypen.drawString("算法:Bubble\n输入:数组data[]\n输出:按非降序排列的数组data[]\n",50,50);
mypen.drawString("1.for i<—data.length-1 to 0",50,80);
mypen.drawString("2. for j<— 0 to i",50,90);
mypen.drawString("3. if a[j] > a[j+1] then",50,100);
mypen.drawString("4. 交换a[j] 与 a[j+1]",50,110);
mypen.drawString("5. end if",50,120);
mypen.drawString("6. end for",50,130);
mypen.drawString("7.end for",50,140);
break;
case 2: mypen.setColor(Color.black);
mypen.drawString("算法:Selection\n输入:数组data[]\n输出:按非降序排列的数组data[]\n",50,50);
mypen.drawString("1.for i<—data.length-1 to 0",50,80);
mypen.drawString("2. T<—0",50,90);
mypen.drawString("3. for j<— 1 to i",50,100);
mypen.drawString("4. if a[j] > a[T] then",50,110);
mypen.drawString("5. T<—j",50,120);
mypen.drawString("5. end if",50,130);
mypen.drawString("6. end for",50,140);
mypen.drawString("7. 交换 a[i] 与 a[T]",50,150);
mypen.drawString("8.end for",50,160);break;
}
switch(set){
case 1:Bubble(data);break;
case 2:Selection(data);break;
}
}

public void pause(){ //延时
if(stop){return;}
try {
Thread.sleep(delayTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(stop){return;}
}
//冒泡排序
public void Bubble(int a[]){
for (int i = a.length; --i>=0;){
if(mypen.getColor()==Color.red) mypen.setColor(Color.blue);
else mypen.setColor(Color.red);

mypen.drawString("1.for i<—data.length-1 to 0",50,80);
for (int j = 0; j <i; j++){
if(mypen.getColor()==Color.red) mypen.setColor(Color.blue);
else mypen.setColor(Color.red);

mypen.drawString("2. for j<— 0 to i",50,90);
mypen.drawString("3. if a[j] > a[j+1]",50,100);
if(stop){return;}
pause();
if (a[j] > a[j+1]) {
int T = a[j];
a[j] = a[j+1];
a[j+1] = T;
mypen.drawString("4. 交换a[j] 与 a[j+1]",50,110);
pause();
}
mypen.drawString("6. end for",50,130);
}
mypen.drawString("6. end for",50,120);
}
mypen.drawString("7.end for",50,130);
}

//选择排序
public void Selection(int a[]){

for (int i = a.length-1; i > 0; i--) {

if(mypen.getColor()==Color.red) mypen.setColor(Color.blue);
else mypen.setColor(Color.red);

mypen.drawString("1.for i<—data.length-1 to o",50,80);
mypen.drawString("2. T<—0",50,90);
int T = 0;

for (int j = 1; j <= i; j++) {

if(stop){return;}
mypen.drawString("3. for j<— 1 to i",50,100);
mypen.drawString("4. if a[j] > a[T] then",50,110);
pause();
if(a[j] > a[T]) {

T = j;

mypen.drawString("5. T<—j",50,120);
}

mypen.drawString("5. end if",50,130);
mypen.drawString("6. end for",50,140);
}

int temp = a[i];

a[i] = a[T];

a[T] = temp;


mypen.drawString("7. 交换 a[i] 与 a[T]",50,150);
pause();
}

mypen.drawString("8.end for",50,160);
}
}
大虾们,帮忙改一下,或重新帮忙写一个,功能就是显示算法的执行步骤。
...全文
95 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
shili150 2008-04-24
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 cnjzy0106 的回复:]
引用 4 楼 fmzbj 的回复:
请列出问题

[/Quote]
cnjzy0106 2008-04-24
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 fmzbj 的回复:]
请列出问题
[/Quote]
fmzbj 2008-04-23
  • 打赏
  • 举报
回复
请列出问题
haoxiongok 2008-04-23
  • 打赏
  • 举报
回复
帮楼主顶一下
Shine_Panda 2008-04-23
  • 打赏
  • 举报
回复
问题到底出在那啊。
把错误信息贴出来看看。。。。。。。。
kokobox 2008-04-23
  • 打赏
  • 举报
回复
没明白lz的意思....

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧