当前位置: 首页 > 知识库问答 >
问题:

如何打印带有public void testDistanceBetween_AD的vaule

傅砚
2023-03-14

我之所以给出这个问题的源代码,是因为我不能在TrainsTest类中做一个简单的系统输出打印,这让我感到很沮丧。正如您所看到的,我在TrainsTest类中为以下方法打印vaule时遇到了问题;

public void testDistanceBetween_ABC public void testDistanceBetween_AD public void testDistanceBetween_ADC public void testDistanceBetween_AEBCD public void testDistanceBetween_AED public void numRoutesWithin_CC30 public void TestQuals

任何帮助都将不胜感激!谢谢你!

下面是我的节点类

package com.utsavized.trains;

public class Node {
public String name;
public boolean visited;

public Node(String name) {
    this.name = name;
    this.visited = false;
}

@Override
public boolean equals(Object b) {
    if (b == null || b.getClass() != getClass()) {
        return false;
    }
    Node bx = (Node)b;
    return this.name.equals(bx.name);
}

@Override
public int hashCode() {
    if(this.name == null) return 0;
    return this.name.hashCode();
}
}

下面是我的Edge类

public class Edge {

public Node origin;

public Node destination;

public int weight;

public Edge next;

public Edge(Node origin, Node destination, int weight) {
    this.origin         = origin;
    this.destination    = destination;
    this.weight         = weight;
    this.next       = null;
}

public Edge next(Edge edge) {
    this.next = edge;
    return this;
}
}
import java.util.ArrayList;
import java.util.Hashtable;

public class Routes {
public Hashtable<Node, Edge> routeTable;

public Routes() {
    this.routeTable = new Hashtable<Node, Edge>();
}


public int distanceBetween(ArrayList<Node> cities) throws Exception {
    /*There is no distance between
     * no cities or 1 city*/
    if(cities.size() < 2)
        return 0;
    int distance, depth, i;
    distance = depth = i = 0;

    /* For each city in the list,
     * we check if entry exists in our
     * hash table.
     */
    while(i < cities.size() - 1) {
        if(this.routeTable.containsKey(cities.get(i))) {
            Edge route = this.routeTable.get(cities.get(i));
            /*If key exists, we check if route from key to next
             * city exists. We add the distance, and maintain a
             * depth count
             */
            while(route != null) {
                if(route.destination.equals(cities.get(i + 1))) {
                    distance += route.weight;
                    depth++;
                    break;
                }
                route = route.next;
            }
        }
        else
            throw new Exception("NO SUCH ROUTE");
        i++;
    }
    /*If edge depth is not equal to vertex - 1,
     * then it is safe to assume that one ore more
     * routes do not exist
     */
    if(depth != cities.size() - 1)
        throw new Exception("NO SUCH ROUTE");

    return distance;
}

/*
 * Number of stops;
 * Wrapper for recursive function
 */
public int numStops(Node start, Node end, int maxStops) throws Exception{
    //Wrapper to maintain depth of traversal
    return findRoutes(start, end, 0, maxStops);
}

/*
 * Finds number of stops from start to end,
 * with a maximum of maxStops and the depth
 * limit.
 */
private int findRoutes(Node start, Node end, int depth, int maxStops) throws Exception{
    int routes = 0;
    //Check if start and end nodes exists in route table
    if(this.routeTable.containsKey(start) && this.routeTable.containsKey(end)) {
        /*
         * If start node exists then traverse all possible
         * routes and for each, check if it is destination
         * If destination, and number of stops within 
         * allowed limits, count it as possible route.
         */
        depth++;
        if(depth > maxStops)        //Check if depth level is within        limits
            return 0;
        start.visited = true;       //Mark start node as visited
        Edge edge = this.routeTable.get(start);
        while(edge != null) {
            /* If destination matches, we increment route
             * count, then continue to next node at same depth
             */
            if(edge.destination.equals(end)) {
                routes++;
                edge = edge.next;
                continue;
            }
            /* If destination does not match, and
             * destination node has not yet been visited,
             * we recursively traverse destination node
             */
            else if(!edge.destination.visited) {
                routes += findRoutes(edge.destination, end, depth,                                                        maxStops);
                depth--;
            }
            edge = edge.next;
        }
    }
    else
        throw new Exception("NO SUCH ROUTE");

    /*
     * Before exiting this recursive stack level,
     * we mark the start node as visited.
     */
    start.visited = false;
    return routes;
}

/*
 * Shortest route;
 * Wrapper for recursive function
 */
public int shortestRoute(Node start, Node end) throws Exception {
    //Wrapper to maintain weight
    return findShortestRoute(start, end, 0, 0);

}

/*
 * Finds the shortest route between two nodes
 */
private int findShortestRoute(Node start, Node end, int weight, int shortestRoute) throws Exception{
    //Check if start and end nodes exists in route table
    if(this.routeTable.containsKey(start) && this.routeTable.containsKey(end)) {
        /*
         * If start node exists then traverse all possible
         * routes and for each, check if it is destination
         */
        start.visited = true;       //Mark start node as visited
        Edge edge = this.routeTable.get(start);
        while(edge != null) {
            //If node not already visited, or is the destination, increment weight
            if(edge.destination == end || !edge.destination.visited)
                weight += edge.weight;

            /* If destination matches, we compare
             * weight of this route to shortest route
             * so far, and make appropriate switch
             */
            if(edge.destination.equals(end)) {
                if(shortestRoute == 0 || weight < shortestRoute)
                    shortestRoute = weight;
                start.visited = false;
                return shortestRoute;           //Unvisit node and return shortest route
            }
            /* If destination does not match, and
             * destination node has not yet been visited,
             * we recursively traverse destination node
             */
            else if(!edge.destination.visited) {
                shortestRoute = findShortestRoute(edge.destination, end, weight, shortestRoute);
                //Decrement weight as we backtrack
                weight -= edge.weight;
            }
            edge = edge.next;
        }
    }
    else
        throw new Exception("NO SUCH ROUTE");

    /*
     * Before exiting this recursive stack level,
     * we mark the start node as visited.
     */
    start.visited = false;
    return shortestRoute;

}

/*
 * Shortest route;
 * Wrapper for recursive function
 */
public int numRoutesWithin(Node start, Node end, int maxDistance) throws Exception {
    //Wrapper to maintain weight
    return findnumRoutesWithin(start, end, 0, maxDistance);
}

/*
 * Finds the shortest route between two nodes
 */
private int findnumRoutesWithin(Node start, Node end, int weight, int maxDistance) throws Exception{
    int routes = 0;
    //Check if start and end nodes exists in route table
    if(this.routeTable.containsKey(start) && this.routeTable.containsKey(end)) {
        /*
         * If start node exists then traverse all possible
         * routes and for each, check if it is destination
         */
        Edge edge = this.routeTable.get(start);
        while(edge != null) {
            weight += edge.weight; 
            /* If distance is under max, keep traversing
             * even if match is found until distance is > max
             */
            if(weight <= maxDistance) {
                if(edge.destination.equals(end)) {
                    routes++;
                    routes += findnumRoutesWithin(edge.destination, end, weight, maxDistance);
                    edge = edge.next;
                    continue;
                }
                else {
                    routes += findnumRoutesWithin(edge.destination, end, weight, maxDistance);
                    weight -= edge.weight;  //Decrement weight as we backtrack
                }
            }
            else 
                weight -= edge.weight;

            edge = edge.next;
        }
    }
    else
        throw new Exception("NO SUCH ROUTE");

    return routes;

}   

下面是我最练的课

    package com.utsavized.trains;



    import static org.junit.Assert.*;

    import java.util.ArrayList;
    import java.util.stream.Collectors;

    import org.junit.BeforeClass;
    import org.junit.Test;

    public class TrainsTest {
static Routes graph;
static Node a, b, c, d, e;

@BeforeClass
public static void setUpBeforeClass() throws Exception {
    graph = new Routes(); //Build graph

    a = new Node("A");
    b = new Node("B");
    c = new Node("C");
    d = new Node("D");
    e = new Node("E");

    /*Input given in programming challenge
    Graph: AB5, BC4, CD8, DC8, DE6, AD5, CE2, EB3, AE7*/
    graph.routeTable.put(a, new Edge(a, b, 5).next(new Edge(a, d, 5).next(new Edge(a, e, 7))));
    graph.routeTable.put(b, new Edge(b, c, 4));
    graph.routeTable.put(c, new Edge(c, d, 8).next(new Edge(c, e, 2)));
    graph.routeTable.put(d, new Edge(d, c, 8).next(new Edge(d, e, 6)));
    graph.routeTable.put(e, new Edge(e, b, 3));
}

@Test
public void testDistanceBetween_ABC() throws Exception {
    ArrayList<Node> route = new ArrayList<Node>(); 
    route.add(a);
    route.add(b);
    route.add(c);
    //System.out.println(a);
    assertEquals(9, graph.distanceBetween(route));      

}

@Test
public void testDistanceBetween_AD() throws Exception {
    ArrayList<Node> route = new ArrayList<Node>(); 
    route.add(a);
    route.add(d);
    assertEquals(5, graph.distanceBetween(route));
}

@Test
public void testDistanceBetween_ADC() throws Exception  {
    ArrayList<Node> route = new ArrayList<Node>(); 
    route.add(a);
    route.add(d);
    route.add(c);
    assertEquals(13, graph.distanceBetween(route));
}

@Test
public void testDistanceBetween_AEBCD() throws Exception  {
    ArrayList<Node> route = new ArrayList<Node>(); 
    route.add(a);
    route.add(e);
    route.add(b);
    route.add(c);
    route.add(d);
    assertEquals(22, graph.distanceBetween(route));
}

@Test(expected=Exception.class)
public void testDistanceBetween_AED() throws Exception  {
    ArrayList<Node> route = new ArrayList<Node>(); 
    route.add(a);
    route.add(e);
    route.add(d);
    assertEquals(-1, graph.distanceBetween(route));
}

@Test
public void testNumStops_CC3() throws Exception {
    int numStops = graph.numStops(c, c, 3);
    assertEquals(2, numStops);
}

@Test
public void testNumStops_AC4() throws Exception {
    int numStops = graph.numStops(a, c, 4);
    assertEquals(4, numStops);
}

@Test
public void testShortestRoute_AC() throws Exception {
    int shortestRoute = graph.shortestRoute(a, c);
    assertEquals(9, shortestRoute);
    System.out.println(shortestRoute);  
}

@Test
public void testShortestRoute_BB() throws Exception {
    int shortestRoute = graph.shortestRoute(b, b);
    assertEquals(9, shortestRoute);
}

@Test
public void numRoutesWithin_CC30() throws Exception {
    int numRoutesWithin = graph.numRoutesWithin(c, c, 30);
    assertEquals(7, numRoutesWithin);
}

@Test
public void testEquals() {
    Node a1 = new Node("A");
    Node a2 = new Node("A");
    Node b = new Node("B");

    assertEquals(true, a1.equals(a2));
    assertEquals(false, a1.equals(b));
    assertEquals(true, (new Node("Test").equals(new Node("Test"))));
}

}

嗨,我正在打印a、b和C的值。正如您所看到的,我在路由上使用了System out println,但它没有输出任何内容。有没有一种方法可以在不对方法进行太多修改的情况下输出结果?

public void testDistanceBetween_ADC() throws Exception  {
    ArrayList<Node> route = new ArrayList<Node>(); 
    route.add(a);
    route.add(d);
    route.add(c);
    assertEquals(13, graph.distanceBetween(route));
    System.out.println(route);
}

共有1个答案

仲孙阳
2023-03-14

您有三种选择

如果Node类是源代码的一部分,只需添加ToString方法

public String toString(){
    return name;
}

如果node类是外部库的一部分但不是最终的,您可以重写该节点并实现ToString()而不是按照supahupe的建议使用overriden类。

System.out.println(route.stream().map(n -> n.name).collect(Collectors.toList()));

或者,您可以为您的节点创建一个装饰器类,其唯一目的是将其很好地打印出来,aproach将与stream相同

...stream().map(n -> new NodeDecorator(n))...
 类似资料:
  • 问题内容: 使用时,Double将添加逗号(5143打印为5,143)。如何禁用逗号? 问题答案: 您的问题属于Rorick正确指出的语言环境。但是,您应该查看DecimalFormat类,以防更改Locale意味着弄乱了所有东西。 查看NumberFormat类,以处理千位分隔符。因为看来您的情况是关于千位分隔符。

  • 我正在尝试让selenium按下打印弹出的保存按钮来保存pdf文件。我可以让selenium按下“print”按钮,但一旦弹出窗口出现adress chrome://print/就不会发生任何事情。还有别的办法吗? 到目前为止,这是零件的代码:

  • 问题内容: 我正在为具有内置行式打印机的Android设备开发应用程序。我必须与此打印机进行交互,并使用它来打印收据上的详细信息,然后使用裁纸器自动剪切该收据。我已经在其中看到了一些ESC命令,但是我不知道如何执行这些ESC命令。 关于Casio设备打印机,我有三个主要问题: 1.我已经使用了内置打印机的打印代码,但是打印后切纸刀没有激活 2.我不知道如何在android中将ESC命令发送到打印机

  • 我使用select.select()代替输入,因为我想要输入超时。我在print()函数中使用end参数,因为我希望我的终端有这样一行: 类型 相反,我没有看到“类型” 我的代码: 我正在使用此脚本测试select.select()并打印(str,end=“”)。我阅读了这篇文章(如何在打印语句后抑制换行?)和这两个命令的官方Python3文档。

  • 问题内容: 搜索过,但没有找到满意的答案。 我知道没有可移植的方式来打印pthread_t。 您如何在您的应用程序中做到这一点? 更新: 实际上,我不需要pthread_t,但是需要一些小的数字ID,以便在调试消息中标识不同的线程。 在我的系统(64位RHEL 5.3)上,它被定义为unsigned long int,因此它的数量很大,仅打印它就在调试行中占据了宝贵的位置。 gdb 如何 分配 短

  • 我在代码中使用熊猫,同时使用下面的代码处理一些数据帧: 我得到这个错误: Traceback(最近的调用最后): 文件“DIST\FDmanInfo.py”,第206行,在 文件“DIST\FDmanInfo.py”,主目录第191行 文件“\local\Python\lib\site packages\pandas\core\generic.py”,第3054行,在astype raise\u