约瑟夫环的程序嵌进界面问题 急!求指导
这是一个约瑟夫环的问题,程序和 界面都已经做出来了 但是不知道怎么合起来 求指导:这是 约瑟夫环的程序:public person(int password, int number) {
this.password = password;
this.number = number;
}
public person(int number){
this.number = number;
}
}
public int ListLength(List<person> list) {
int count = 0;
if (list != null) {
for (person p : list) {
if (p.state != 0) {
count++;
}
}
}
return count;
}
public void cacle() {
// 初始化数据
List<person> list = new LinkedList<person>();
Scanner in = new Scanner(System.in);
System.out.print("请输入人数n:");
int n = in.nextInt();
for(int i=0;i<n;i++){
int a, b;
a = in.nextInt();
b = in.nextInt();
list.add(new person(a,b));
}
// list.add(new person(4,7));
int position = -1;//初始位置
System.out.print("请输入第一次报多少的人出来m:");
int m = in.nextInt(); //第一次报多少的人出来
int count = 0;//已经报了多少人
while (ListLength(list) != 0) {
position = (position + 1) % list.size();// 位置定位
if (((person) list.get(position)).state != 0) {
count++;
}
if (count == m) {
person p = list.get(position);
System.out.print(p.number+" ");
p.state = 0;
m = p.password;
list.set(position, p);
count = 0;
}
}
}
public static void main(String[] args) {
Question2 q= new Question2();
q.cacle();
}
}
这是界面:
import java.awt.*;
import java.awt.event.*;
public class Question2Frame extends Frame implements ActionListener
{
private TextField text_1,text_2,text_3,text_4;
private Button button_1;
public Question2Frame()
{
super("约瑟夫环编辑器");
this.setBounds(280,240,300,300);
this.setBackground(java.awt.Color.cyan);
this.setLayout(new java.awt.FlowLayout(FlowLayout.LEFT));
this.add(new Label("人数n:"));
text_1=new TextField("7",40);
this.add(text_1);
this.add(new Label("请输入每个人的密码,并用空格隔开:"));
text_2=new TextField(40);
this.add(text_2);
this.add(new Label("第一个密码m:"));
text_3=new TextField("20",40);
this.add(text_3);
this.add(new Label("输出结果为:"));
text_4=new TextField(40);
this.add(text_4);
button_1=new Button("OK");
this.add(button_1);
this.addWindowListener(new WinClose());
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
}
public static void main(String arg[])
{
new Question2Frame();
}
}
class WinClose implements WindowListener
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void windowOpened(WindowEvent e){ }
public void windowActivated(WindowEvent e){ }
public void windowDeactivated(WindowEvent e){ }
public void windowClosed(WindowEvent e){ }
public void windowIconified(WindowEvent e){ }
public void windowDeiconified(WindowEvent e){ }
}
求结合 !!