比如這是一個(gè)八皇后的程序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);}怎么修改代碼使得通過(guò)按下"NEXT' 按鈕讓APPLET顯示問(wèn)題的下一個(gè)解
怎么修改代碼使得通過(guò)按下"NEXT' 按鈕讓APPLET顯示問(wèn)題的下一個(gè)解?
阿波羅的戰(zhàn)車
2022-05-18 12:11:12