接受用户定义的数据类型的输入



我正在编写一个程序来实现Dijkstra算法(图中一个节点和其他节点之间的最短路径)。我希望用户作为输入给出的源节点被读取为顶点(用户定义的数据类型)。

以下是我的程序:

import java.util.*;
class Vertex implements Comparable<Vertex>
{
    public final String name;
    public Edge[] adjacencies;
    public double minDistance = Double.POSITIVE_INFINITY;
    public Vertex previous;
    public Vertex(String argName) { 
        name = argName;
    }
    public String toString() {
        return name;
    }
    public int compareTo(Vertex other) {
        return Double.compare(minDistance, other.minDistance);
    }
}
class Edge
{
    public final Vertex target;
    public final double weight;
    public Edge(Vertex argTarget, double argWeight) { 
        target = argTarget;
        weight = argWeight;
    }
}
public class DijExample
{
    public static void computePaths(Vertex source) {
        /*This function computes the shortest distance*/
    }
    public static List<Vertex> getShortestPathTo(Vertex target){
        /*This function computes the shortest path*/
    }
    public static void main(String[] args){
        in = new Scanner(System.in);
        String sn;
        Vertex v0 = new Vertex("S");
        Vertex v1 = new Vertex("A");
        Vertex v2 = new Vertex("B");
        Vertex v3 = new Vertex("C");
        Vertex v4 = new Vertex("D");
        Vertex v5 = new Vertex("E");
        Vertex v6 = new Vertex("F");
        Vertex v7 = new Vertex("G");
        Vertex v8 = new Vertex("H");
        Vertex v9 = new Vertex("I");
        Vertex v10 = new Vertex("J");
        Vertex v11 = new Vertex("K");
        Vertex v12 = new Vertex("L");

        v0.adjacencies = new Edge[]{new Edge(v1,5),new Edge(v2,7),new Edge(v3,1),new Edge(v4,3),new Edge(v5,2),new Edge(v6,6)};
        v1.adjacencies = new Edge[]{new Edge(v0,5),new Edge(v5,4),new Edge(v6,7)};
        v2.adjacencies = new Edge[]{new Edge(v0,7),new Edge(v10,6),new Edge(v11,2)};
        v3.adjacencies = new Edge[]{new Edge(v0,1), new Edge(v11,1)};
        v4.adjacencies = new Edge[]{new Edge(v0,3),new Edge(v7,7)};
        v5.adjacencies = new Edge[]{new Edge(v0,2),new Edge(v8,5)};
        v6.adjacencies = new Edge[]{new Edge(v0,6),new Edge(v1,7),new Edge(v9,3)};
        v7.adjacencies = new Edge[]{new Edge(v4,7)};
        v8.adjacencies = new Edge[]{new Edge(v5,5)};
        v9.adjacencies = new Edge[]{new Edge(v6,3)};
        v10.adjacencies = new Edge[]{new Edge(v2,6),new Edge(v11,8),new Edge(v12,1)};
        v11.adjacencies = new Edge[]{new Edge(v2,2),new Edge(v3,1),new Edge(v10,8),new Edge(v12,3)};
        v12.adjacencies = new Edge[]{new Edge(v10,1),new Edge(v11,3)};
        Vertex[] vertices = { v0,v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12 };
        System.out.println("Enter the source node");
        sn = in.next();//Here sn is read as a string but I want to read it as a vertex
        computePaths(sn);//Calling the function to calculate the shortest path with the source node (of type vertex)
        for (Vertex v : vertices){
            System.out.println("Distance to " + v + ": " + v.minDistance);
            List<Vertex> path = getShortestPathTo(v);
            System.out.println("Path: " + path);
        }
    }
} 

我假设您的问题是将用户输入转换为自定义类型,而不是算法本身。琐碎的答案是改变你的:

sn = in.next();

Vertex vn = new Vertex (in.next());

这会给你一个新的顶点,但它不在你构建的arry中,而且你没有一个简单的方法来找到是否有匹配的顶点。您也会受到不良输入的影响。

一种更有用的机制可能是将顶点集合更改为某种类型的Map(可能是HashMap<String,Vertex>),在Vertex.name上键入。

你可以像Vertices.add (v0.name, v0); 一样将你的Vertex对象添加到其中

然后你可以检查用户是否指定了一个有效的顶点-返回到读取字符串:

Vertex vn = null;
sn = in.next();

if (Vertices.contains(sn)) {
  vn = Vertices.get(sn);
} else {
  System.out.println ("That's not a vertex I know about");
  System.exit(1);
}

相关内容

  • 没有找到相关文章

最新更新