我有一個eclipse的問題,它說圖形不能解析爲一個類型,它非常混亂。它是我在一本教科書的幫助下編寫的大學課程的一部分。問題似乎無處不在,我有DistPar,sPath和頂點列表。任何人都可以幫忙圖形無法解析爲類型eclipse錯誤
public class shortpa {
public int distance;
public int parentVert;
public char label;
public boolean isInTree;
public final int MAX_VERTS=20;
public final int INFINITY=1000000;
public Vertex vertexList[];
public int adjMat[][];
public int nVerts;
public int nTree;
public DistPar sPath[];
public int currentVert;
public int startToCurrent;
public static void main(String[] args)
{
Graph theGraph = new Graph();//error here saying graph can nt be resolved to a type
theGraph.addVertex('A');
theGraph.addVertex('B');
theGraph.addVertex('C');
theGraph.addVertex('D');
theGraph.addVertex('Z');
theGraph.addEdge(1, 2, 10);//1 repesents A, 2 repesents B....etc and 10 is the weight
theGraph.addEdge(1, 5, 18);
theGraph.addEdge(2, 3, 3);
theGraph.addEdge(2, 5,17);
theGraph.addEdge(2, 4, 1);
theGraph.addEdge(3, 4, 1);
theGraph.addEdge(4, 5, 4);
System.out.println("Shortest paths");
theGraph.path();
System.out.println();
}
//--------------------------------------------------------------------------
public void DistPar(int pv, int d)
{
distance = d;
parentVert=pv;
}
//--------------------------------------------------------------------------
public void Vertex(char lab)
{
label = lab;
isInTree = false;
}
//--------------------------------------------------------------------------
public void Graph()
{
vertexList = new Vertex[MAX_VERTS];/////error here saying vertex can not be resolved to a type
adjMat = new int[MAX_VERTS][MAX_VERTS];
nVerts=0;
nTree=0;
for(int j =0; j<MAX_VERTS; j++)
for(int k=0; k<MAX_VERTS; k++)
adjMat[j][k] = INFINITY;
sPath = new DistPar[MAX_VERTS];//error here saying distpar can not be resolved to a type
}
//--------------------------------------------------------------------------
public void addVertex(char lab)
{
vertexList[nVerts++] = new Vertex(lab);
}
//-------------------------------------------------------------------------
public void addEdge(int start, int end, int weight)////have a look here
{
adjMat[start][end]=weight;
}
//--------------------------------------------------------------------
public void path()
{
int startTree = 0;
vertexList[startTree].isInTree = true;
nTree = 1;
for(int j=0; j<nVerts; j++)
{
int tempDist = adjMat[startTree][j];
sPath[j] = new DistPar(startTree, tempDist);
}
while(nTree < nVerts)
{
int indexMin = getMin();
int minDist = sPath[indexMin].distance;
if(minDist == INFINITY)
{
System.out.println("Unreachable vertexs");
break;
}
else
{
currentVert=indexMin;
startToCurrent=sPath[indexMin].distance;
}
vertexList[currentVert].isInTree = true;
nTree++;
adjust_sPath();
}
displayPaths();
nTree = 0;
for(int j=0; j<nVerts; j++)
vertexList[j].isInTree = false;
}
//---------------------------------------------------------------------------
public int getMin()
{
int minDist = INFINITY;
int indexMin = 0;
for(int j=1; j<nVerts;j++)
{
if(!vertexList[j].isInTree && sPath[j].distance < minDist)
{
minDist = sPath[j].distance;
indexMin = j;
}
}
return indexMin;
}
//------------------------------------------------------------------------------
public void adjust_sPath()
{
int column = 1;
while(column < nVerts)
{
if(vertexList[column].isInTree)
{
column++;
continue;
}
int currentToFringe = adjMat[currentVert][column];
int startToFringe = startToCurrent + currentToFringe;
int sPathDist = sPath[column].distance;
if(startToFringe < sPathDist)
{
sPath[column].parentVert = currentVert;
sPath[column].distance = startToFringe;
}
column++;
}
}
//------------------------------------------------------------------------------
public void displayPaths()
{
for(int j=0; j< nVerts; j++)
{
System.out.println(vertexList[j].label+"=");
if(sPath[j].distance == INFINITY)
System.out.println("inf");
else
System.out.println(sPath[j].distance);
char parent = vertexList[sPath[j].parentVert].label;
System.out.println(" (" + parent + ") ");
}
System.out.println("");
}
}
謝謝,現在我的公共靜態void main不被識別。但是這有助於歡呼 – ryan1234