比如这是一个八皇后的程序package com.equeen;
import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Event;
import java.awt.Graphics;
public class Queen extends Applet{
/**
*
*/
private static final long serialVersionUID = 1L;
int num;
int n;
Button b1 = new Button("next");
int cross(int k,int l[])
{
int j;
if(k==1) return 0;
for(j=1;j<=k-1;j++){
if(l[j]==l[k]||(j-k==l[j]-l[k])||(k-j==l[j]-l[k])) return 1;
}
return 0;
}
public void init(){
this.resize(400, 400);
add(b1);
}
public boolean action(Event e, Object o1) {
if(e.target==b1) try{Thread.interrupted();}catch(Exception c){}
return true;
}
public void fill(int k,int l[],Graphics g)
{ int i;
if(k<=n)
for(l[k]=1;l[k]<=n;l[k]++){
if(cross(k,l)==0) fill(k+1,l,g);
}
else {
num++;
g.setColor(new Color(0,0,0));
for(i=1;i<=9;i++){
g.drawLine(40, i*40, 360, i*40);
g.drawLine(i*40, 40, i*40, 360);
}
for(i=1;i<=8;i++) g.fillRect(l[i]*40, i*40, 40, 40);
try{Thread.sleep(2000);}catch(Exception e){
}
g.clearRect(40, 40, 360, 360);
}
}
public void paint(Graphics g) {
int l[]=new int[15];
Queen q=new Queen();
q.num=0;
q.n=8;
q.fill(1,l,g);
System.out.println(q.num);
}
怎么修改代码使得通过按下"NEXT' 按钮让APPLET显示问题的下一个解
炎炎设计
相关分类