我已经写好了两个类,一个是用来显示界面的,一个是用来产生路径走法的,但是路径走法这个类还有问题,我目前是写死的,只是关于两个确定的点,我想用 Scanner输入,我输入哪两个站点,就显示哪两个站点的走法,这个如何实现,我试了好久也不行,求大神指教,最好有完整的代码,本人新手,真心会的东西不多。
界面的代码:
package work;import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.util.*;public class Interface
{
private Frame f;
private TextField tf;
private TextField tf2;
private Label lab1;
private Label lab2;
private Label lab3;
private Button b;
private TextArea ta;

public Interface()
{
init();
}

public void init()
{
f = new Frame("my frame");

f.setBounds(300, 100, 600, 500);
f.setBackground(Color.green);
f.setLayout(new FlowLayout());
lab1 = new Label("起点");
lab2 = new Label("到");
lab3 = new Label("终点");
tf = new TextField(20);
tf2 = new TextField(20);
b = new Button("查询");
ta = new TextArea(25,70);

f.add(lab1);
f.add(tf);
f.add(lab2);

f.add(tf2);
f.add(lab3);
f.add(b);
f.add(ta);

myEvent();




f.setVisible(true);
}

private void myEvent()
{
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e) //匿名内部类
{
System.exit(0);
}
}
);

tf.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_ENTER)
showDir();
}
});
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
showDir();
}
});
}

private void showDir()
{
String dirPath = tf.getText();
File dir = new File(dirPath);
if(dir.exists() && dir.isDirectory())
{
ta.setText("");
String[] names = dir.list();
for(String name : names)
{
ta.append(name+"\r\n");
}
}
}

public static void main(String[] args)
{
new Interface();
}
}
路径走法代码
package work;public class HignWay 
{
//存储路径(字符串,用于打印输出)
private static StringBuffer goToTheEnd = new StringBuffer();

//存储路径(站内的对象,size可以自行调整)
private static Station stationArr[] = new Station[10];
private static int index = 0; //stationArr[]的下标
private static int count = 0; //路径总数计数

private void funcStartToEnd(Station start, Station end)
{
stationArr[index++] = start; //保存起始站
int i = 0;

if(!start.equals(end))//起始站和终点站不是同一个站
{
//向终点站出发,逐个遍历周围能直达的站
for(;i < start.nextStation.length;i++)
{
if(start.nextStationFlag[i] == 0 )//flag == 0 表示有下一站可以走
{
int k = 0;
for(;k<start.nextStation[i].nextStationFlag.length;k++)
{
/*
 对下一站进行初期化,把该站周围的直达站
 (即start的下一站的再下一站)的flag全部置成0,
  防止路线重叠冲突*/
start.nextStation[i].nextStationFlag[k] = 0;

/* 对下一站周边的直达站与之前走过的站对比
如果一样,就设置为1,避免走回头路 */
int m = 0;
for(;stationArr[m] != null;m++)
{
if(start.nextStation[i].nextStation[k].equals(stationArr[m]))
start.nextStation[i].nextStationFlag[k] = 1;
}
}
start.nextStationFlag[i] = 1; //start走向下一站,于是置为1
//到下一站继续寻路
funcStartToEnd(start.nextStation[i], end);
return;
}
}
}
//死路了,连下一站都沒有了,只能往回跳了,此为回溯
index -= 2;
for(i = 0;stationArr[i]!= null;i++)
{
if(stationArr[i].equals(end))
{
showTheRoad(end);
count++;
}
}
stationArr[index+1] = null;
if(index >= 0)
funcStartToEnd(stationArr[index], end);
return;
}

//把路径存储到StringBuffer中并打印
private void showTheRoad(Station end)
{
goToTheEnd.delete(0, goToTheEnd.length());
int i = 0;
for(;stationArr[i]!=null;i++)
{
goToTheEnd.append(stationArr[i].stationName);
goToTheEnd.append(" - ");
if(stationArr[i].equals(end))break;
}

goToTheEnd.delete(goToTheEnd.length()-3, goToTheEnd.length() );
System.out.println(goToTheEnd);
} public static void main(String[] args) 
{
/*可以自行对线路图进行扩展,扩展以后不要忘了初期化,每加一个站,周边的都会受到影响*/

/*田字型路线图*/
/* a--b--c */
/* |  |  | */
/* d--e--f */
/* |  |  | */
/* g--h--i */

/*-------------------对各个站进行初期化-------------------*/
Station a = new Station("站A");
Station b = new Station("站B");
Station c = new Station("站C");
Station d = new Station("站D");
Station e = new Station("站E");
Station f = new Station("站F");
Station g = new Station("站G");
Station h = new Station("站H");
Station i = new Station("站I");

a.nextStationFlag = new int[2];
a.nextStation = new Station[2];
a.nextStation[0] = b;
a.nextStation[1] = d;

b.nextStationFlag = new int[3];
b.nextStation = new Station[3];
b.nextStation[0] = a;
b.nextStation[1] = c;
b.nextStation[2] = e;

c.nextStationFlag = new int[2];
c.nextStation = new Station[2];
c.nextStation[0] = b;
c.nextStation[1] = f;

d.nextStationFlag = new int[3];
d.nextStation = new Station[3];
d.nextStation[0] = a;
d.nextStation[1] = e;
d.nextStation[2] = g;

e.nextStationFlag = new int[4];
e.nextStation = new Station[4];
e.nextStation[0] = b;
e.nextStation[1] = d;
e.nextStation[2] = f;
e.nextStation[3] = h;

f.nextStationFlag = new int[3];
f.nextStation = new Station[3];
f.nextStation[0] = c;
f.nextStation[1] = e;
f.nextStation[2] = i;

g.nextStationFlag = new int[2];
g.nextStation = new Station[2];
g.nextStation[0] = d;
g.nextStation[1] = h;

h.nextStationFlag = new int[3];
h.nextStation = new Station[3];
h.nextStation[0] = e;
h.nextStation[1] = g;
h.nextStation[2] = i;

i.nextStation = new Station[2];
i.nextStationFlag = new int[2];
i.nextStation[0] = f;
i.nextStation[1] = h; //找路开始Start to End
new HignWay().funcStartToEnd(a,h);//目前是写死掉的
System.out.println("总共有" +count+ "种走法");
}
}//站类
class Station
{
public Station(String name) //构造函数,起名用
{
this.stationName = name;
}

public String stationName; //本站名
public Station nextStation[]; //能直达的下一站数组
public int nextStationFlag[]; //能直达的下一站的flag,0能直达,1不能直达,一旦走过这条路,就设为1
}

解决方案 »

  1.   

    我最终想实现的是:
    在界面下,输入 起点的站名,再输入 终点的站名,然后按查询,就能在界面上显示出 查询出来的走法
      

  2.   

    这是公司给我的要求:
    从起点到终点之间的路网可以有多重走法,本程序要求在选择起点和终点之后,可以自动计算从起点到终点之间的路径走法,并在界面中显示具体行驶路径,并且根据各个行驶路径计算应该收的通行费金额。
    我想一步步实现,求大神,最好能有完整的代码
    本人真心不会,都愁了三天了,求完整代码 谢谢了
      

  3.   

    求人指教,跪求,本人在线等,已经三天没睡好了