java - 在公共(public)类中声明私有(private)变量时,java中表达式的非法开始

标签 java

import java.util.InputMismatchException;

    import java.util.Scanner;

    import java.util.Stack;

public class TSPNearestNeighbour {
 {

        private final Stack<Integer> stack;
        private int numberOfNodes;
        public TSPNearestNeighbour()

        {

            stack = new Stack<Integer>();

        }



        public void tsp(int adjacencyMatrix[][])

        {

            numberOfNodes = adjacencyMatrix[1].length - 1;

            int[] visited = new int[numberOfNodes + 1];

            visited[1] = 1;

            stack.push(1);

            int element, dst = 0, i,cost=0;

            int min = Integer.MAX_VALUE;

            boolean minFlag = false;

            System.out.print(1 + "\t");



            while (!stack.isEmpty())

            {

                element = stack.peek();

                i = 1;

                min = Integer.MAX_VALUE;

                while (i <= numberOfNodes)

                {

                    if (adjacencyMatrix[element][i] > 1 && visited[i] == 0)

                    {

                        if (min > adjacencyMatrix[element][i])

                        {

                            min = adjacencyMatrix[element][i];

            cost=cost+adjacencyMatrix[element][i];

                            dst = i;

                            minFlag = true;

                        }

                    }

                    i++;

                }

                if (minFlag)

                {

                    visited[dst] = 1;

                    stack.push(dst);

                    System.out.print( dst + "\t");


                    minFlag = false;

                    continue;

                }

                stack.pop();

            }
System.out.println("total cost" +cost);
        }



        public static void main(String args[])

        {

            int number_of_nodes;

            Scanner scanner = null;

            try

            {

                System.out.println("Enter the number of nodes in the graph");

                scanner = new Scanner(System.in);

                number_of_nodes = scanner.nextInt();

                int adjacency_matrix[][] = new int[number_of_nodes + 1][number_of_nodes + 1];

                System.out.println("Enter the adjacency matrix");

                for (int i = 1; i <= number_of_nodes; i++)

                {

                    for (int j = 1; j <= number_of_nodes; j++)

                    {

                        adjacency_matrix[i][j] = scanner.nextInt();

                    }

                }

                for (int i = 1; i <= number_of_nodes; i++)

                {

                    for (int j = 1; j <= number_of_nodes; j++)

                    {

                        if (adjacency_matrix[i][j] == 1 && adjacency_matrix[j][i] == 0)

                        {

                            adjacency_matrix[j][i] = 1;

                        }

                    }

                }

                System.out.println("the citys are visited as follows");

                TSPNearestNeighbour tspNearestNeighbour = new TSPNearestNeighbour();

                tspNearestNeighbour.tsp(adjacency_matrix);

            } catch (InputMismatchException inputMismatch)

             {

                 System.out.println("Wrong Input format");

             }

            scanner.close();

        }

    }    



> illegal start of expression in the line:
>       **private final Stack<Integer> stack;**

最佳答案

你有 2 个左大括号

public class TSPNearestNeighbour { {

删除一个,也许你就能编译代码

关于java - 在公共(public)类中声明私有(private)变量时,java中表达式的非法开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24094353/

相关文章:

java - Java BouncyCaSTLe 中的确定性 AES-CTR?

java - 与导航主机 fragment 大小不同的 fragment

Java 8 - 无法在数组类型 Enum[] 上调用stream()

java - 使用 ResultSetTableModel 删除 JTable 选中的行

java - 十六进制编码字符串到字节数组

java - 从多个数字 ID 创建唯一的数字 ID

java - 在 Visual VM 中限制分析

java - 如何有效地搜索多个数组中的项目?

java - 如何在Java中创建通用数组?

java - 致命信号 6 (SIGABRT)