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

如何创建所有方法都可以访问的公共数组,但让用户输入决定它的大小?

夏骞尧
2023-03-14

我有多个数组,它们的大小需要由用户输入来决定。这些数组应该可以在main方法和stepTwo()方法中访问。但是,我卡住了。用户输入直到main方法才出现,但是如果我在main方法中声明了数组,那么我就不能访问stepTwo()方法中的数组。我不喜欢将数组作为参数传递给stepTwo(),因为我以前尝试过,但出现了多个错误。有什么建议吗?完整代码见下文:

    public class AssignmentIII
    {       
    public static int numProcesses; // Represents the number of processes
    public static int numResources; // Represents the number of different types of resources

    public static int[] available = new int[numResources]; // Create an emptry matrix for available processes
    public static int[][] allocation = new int[numProcesses][numResources]; // Create an empty allocation matrix nxm
    public static int[][] request = new int[numProcesses][numResources]; // Create an empty request matrix nxm
    public static int[] work = new int[numResources]; // Create an empty work matrix
    public static Boolean[] finish = new Boolean[numProcesses]; // Create an empty finish matrix

    public static void main(String[] args) throws FileNotFoundException
    {
        try
        {
            Scanner scan = new Scanner(System.in);
            Scanner fileScan = new Scanner(new File("input1.txt")); // Create file scanner

            System.out.println("Please enter the total number of processes: ");
            numProcesses = scan.nextInt();
            System.out.println("Please enter the number of different types of resources: ");
            numResources = scan.nextInt();

            // Initialize the available matrix
            for(int i = 0; i < numResources; i++)
            available[i]=fileScan.nextInt();

            // Initialize the allocation matrix
            for(int j = 0; j < numProcesses; j++)
                for(int k = 0; k < numResources; k++)
                    allocation[j][k]=fileScan.nextInt();

            // Initialize the request matrix
            for(int m = 0; m < numProcesses; m++)
                for(int n = 0; n < numResources; n++)
                    request[m][n]=fileScan.nextInt();

            // Print allocation matrix
            System.out.println();
            System.out.println("Allocated");
            for(int i = 0; i < numResources; i++)
            {
                System.out.print("\tR" + i);
            }
            System.out.println();
            for(int j = 0; j < numProcesses; j++)
            {
                System.out.print("P" + j);
                for(int k = 0; k < numResources; k++)
                {
                    System.out.print("\t" + allocation[j][k]);
                }
                System.out.println();
            }

            // Print available matrix
            System.out.println();
            System.out.println("Available");
            for(int i = 0; i < numResources; i++)
            {
                System.out.print("R" + i + "\t");
            }
            System.out.println();
            for(int i = 0; i < numResources; i++)
                System.out.print(available[i] + "\t");
            System.out.println();


            // Print request matrix
            System.out.println();
            System.out.println("Requested");
            for(int i = 0; i < numResources; i++)
                {
                    System.out.print("\tR" + i);
                }
            System.out.println();

            for(int m = 0; m < numProcesses; m++)
            {
                System.out.print("P" + m);
                for(int n = 0; n < numResources; n++)
                {
                    System.out.print("\t" + request[m][n]);
                }
                System.out.println();
            }
            System.out.println();

            // Begin deadlock detection algorithm               
            for(int i = 0; i < numResources; i++) // Intialize Work := Available
                work[i]=available[i];

            for(int j = 0; j < numProcesses; j++) // Check for Allocation != 0 and initialize Finish accordingly
            {
                int sumAllocation = 0;
                for(int i = 0; i < numResources; i++)
                {
                    sumAllocation += allocation[j][i];
                }

                if (sumAllocation != 0)
                    finish[j] = false;
                else finish[j] = true;
            }

            stepTwo();

            }
            catch(FileNotFoundException ex)
            {
            System.out.println("An error has occured. The file cannot be found.");
            }
    }

    public static void stepTwo()
    {
        // Step 2
        // Find an index i where Finish[i] = false & Request[i] <= Work         
        for(int i = 0; i < numProcesses; i++)
        {
            int sumRequests = 0;
            int sumWork = 0;

            // Sum the Request and Work vectors
            for(int k = 0; k < numResources; k++)
            {   
                sumRequests += request[i][k];
                sumWork += work[k];
            }

            if (finish[i] == false && sumRequests <= sumWork)
            {
                finish[i] = true;
                for(int m = 0; m < numResources; m++)
                {
                    work[m] = work[m] + allocation[i][m];
                }

                stepTwo();
            }
            else if (finish[i] == false)
            // Step 4: Print which processes are in a deadlock state
            // Print using P0, P1, ... , Pn format
                System.out.println("P" + i + " is in a deadlock state.");                           
        }
    }
 }

共有2个答案

岳泉
2023-03-14

你必须将数组初始化到主块中,就像这样。

public class AssignmentIII
{       
public static int numProcesses; // Represents the number of processes
public static int numResources; // Represents the number of different types of resources


public static int[] available ;
public static int[][] allocation ;
public static int[][] request ;
public static int[] work ;
public static Boolean[] finish ;

public static void main(String[] args) throws FileNotFoundException
{
    try
    {
        Scanner scan = new Scanner(System.in);
        Scanner fileScan = new Scanner(new File("input1.txt")); // Create file scanner

        System.out.println("Please enter the total number of processes: ");
        numProcesses = scan.nextInt();
        System.out.println("Please enter the number of different types of resources: ");
        numResources = scan.nextInt();

      available = new int[numResources]; // Create an emptry matrix for available processes
      allocation = new int[numProcesses][numResources]; // Create an empty allocation matrix nxm
      request = new int[numProcesses][numResources]; // Create an empty request matrix nxm
      work = new int[numResources]; // Create an empty work matrix
      finish = new Boolean[numProcesses]; // Create an empty finish matrix

        // Initialize the available matrix
        for(int i = 0; i < numResources; i++)
        available[i]=fileScan.nextInt();

        // Initialize the allocation matrix
        for(int j = 0; j < numProcesses; j++)
            for(int k = 0; k < numResources; k++)
                allocation[j][k]=fileScan.nextInt();

        // Initialize the request matrix
        for(int m = 0; m < numProcesses; m++)
            for(int n = 0; n < numResources; n++)
                request[m][n]=fileScan.nextInt();

        // Print allocation matrix
        System.out.println();
        System.out.println("Allocated");
        for(int i = 0; i < numResources; i++)
        {
            System.out.print("\tR" + i);
        }
        System.out.println();
        for(int j = 0; j < numProcesses; j++)
        {
            System.out.print("P" + j);
            for(int k = 0; k < numResources; k++)
            {
                System.out.print("\t" + allocation[j][k]);
            }
            System.out.println();
        }

        // Print available matrix
        System.out.println();
        System.out.println("Available");
        for(int i = 0; i < numResources; i++)
        {
            System.out.print("R" + i + "\t");
        }
        System.out.println();
        for(int i = 0; i < numResources; i++)
            System.out.print(available[i] + "\t");
        System.out.println();


        // Print request matrix
        System.out.println();
        System.out.println("Requested");
        for(int i = 0; i < numResources; i++)
            {
                System.out.print("\tR" + i);
            }
        System.out.println();

        for(int m = 0; m < numProcesses; m++)
        {
            System.out.print("P" + m);
            for(int n = 0; n < numResources; n++)
            {
                System.out.print("\t" + request[m][n]);
            }
            System.out.println();
        }
        System.out.println();

        // Begin deadlock detection algorithm               
        for(int i = 0; i < numResources; i++) // Intialize Work := Available
            work[i]=available[i];

        for(int j = 0; j < numProcesses; j++) // Check for Allocation != 0 and initialize Finish accordingly
        {
            int sumAllocation = 0;
            for(int i = 0; i < numResources; i++)
            {
                sumAllocation += allocation[j][i];
            }

            if (sumAllocation != 0)
                finish[j] = false;
            else finish[j] = true;
        }

        stepTwo();

        }
        catch(FileNotFoundException ex)
        {
        System.out.println("An error has occured. The file cannot be found.");
        }
}

public static void stepTwo()
{
    // Step 2
    // Find an index i where Finish[i] = false & Request[i] <= Work         
    for(int i = 0; i < numProcesses; i++)
    {
        int sumRequests = 0;
        int sumWork = 0;

        // Sum the Request and Work vectors
        for(int k = 0; k < numResources; k++)
        {   
            sumRequests += request[i][k];
            sumWork += work[k];
        }

        if (finish[i] == false && sumRequests <= sumWork)
        {
            finish[i] = true;
            for(int m = 0; m < numResources; m++)
            {
                work[m] = work[m] + allocation[i][m];
            }

            stepTwo();
        }
        else if (finish[i] == false)
        // Step 4: Print which processes are in a deadlock state
        // Print using P0, P1, ... , Pn format
            System.out.println("P" + i + " is in a deadlock state.");                           
    }
}

}

但这不是推荐的方式。因为这不是使用静态方法和静态属性的正确方法。此外,您应该使用封装。使用更好的设计和封装方法,您的代码可以得到如下改进。

    public class AssignmentIII
{       
    int numProcesses; // Represents the number of processes
    int numResources; // Represents the number of different types of resources
    String filepath;

    int[] available = new int[numResources]; // Create an emptry matrix for available processes
    int[][] allocation = new int[numProcesses][numResources]; // Create an empty allocation matrix nxm
    int[][] request = new int[numProcesses][numResources]; // Create an empty request matrix nxm
    int[] work = new int[numResources]; // Create an empty work matrix
    Boolean[] finish = new Boolean[numProcesses]; // Create an empty finish matrix

    public AssignmentIII(int numResources,int numProcesses, String filepath){
        this.numProcesses = numProcesses;
        this.numResources = numResources;
        this.filepath = filepath;

        available = new int[numResources]; // Create an emptry matrix for available processes
        allocation = new int[numProcesses][numResources]; // Create an empty allocation matrix nxm
        request = new int[numProcesses][numResources]; // Create an empty request matrix nxm
        work = new int[numResources]; // Create an empty work matrix
        finish = new Boolean[numProcesses]; // Create an empty finish matrix
    }

    public void initilizeMatrix() throws FileNotFoundException{
        Scanner fileScan = new Scanner(new File(filepath)); // Create file scanner

        // Initialize the available matrix
        for(int i = 0; i < numResources; i++)
            available[i]=fileScan.nextInt();

        // Initialize the allocation matrix
        for(int j = 0; j < numProcesses; j++)
            for(int k = 0; k < numResources; k++)
                allocation[j][k]=fileScan.nextInt();

        // Initialize the request matrix
        for(int m = 0; m < numProcesses; m++)
            for(int n = 0; n < numResources; n++)
                request[m][n]=fileScan.nextInt();

        fileScan.close();

    }



    public void print(){
        // Print allocation matrix
        System.out.println();
        System.out.println("Allocated");
        for(int i = 0; i < numResources; i++)
        {
            System.out.print("\tR" + i);
        }
        System.out.println();
        for(int j = 0; j < numProcesses; j++)
        {
            System.out.print("P" + j);
            for(int k = 0; k < numResources; k++)
            {
                System.out.print("\t" + allocation[j][k]);
            }
            System.out.println();
        }

        // Print available matrix
        System.out.println();
        System.out.println("Available");
        for(int i = 0; i < numResources; i++)
        {
            System.out.print("R" + i + "\t");
        }
        System.out.println();
        for(int i = 0; i < numResources; i++)
            System.out.print(available[i] + "\t");
        System.out.println();


        // Print request matrix
        System.out.println();
        System.out.println("Requested");
        for(int i = 0; i < numResources; i++)
        {
            System.out.print("\tR" + i);
        }
        System.out.println();

        for(int m = 0; m < numProcesses; m++)
        {
            System.out.print("P" + m);
            for(int n = 0; n < numResources; n++)
            {
                System.out.print("\t" + request[m][n]);
            }
            System.out.println();
        }
        System.out.println();

    }

    // Begin deadlock detection algorithm               
    public void deadLockdetecter(){
        for(int i = 0; i < numResources; i++) // Intialize Work := Available
            work[i]=available[i];

        for(int j = 0; j < numProcesses; j++) // Check for Allocation != 0 and initialize Finish accordingly
        {
            int sumAllocation = 0;
            for(int i = 0; i < numResources; i++)
            {
                sumAllocation += allocation[j][i];
            }

            if (sumAllocation != 0)
                finish[j] = false;
            else finish[j] = true;
        }
    }

    public  void stepTwo()
    {
        // Step 2
        // Find an index i where Finish[i] = false & Request[i] <= Work         
        for(int i = 0; i < numProcesses; i++)
        {
            int sumRequests = 0;
            int sumWork = 0;

            // Sum the Request and Work vectors
            for(int k = 0; k < numResources; k++)
            {   
                sumRequests += request[i][k];
                sumWork += work[k];
            }

            if (finish[i] == false && sumRequests <= sumWork)
            {
                finish[i] = true;
                for(int m = 0; m < numResources; m++)
                {
                    work[m] = work[m] + allocation[i][m];
                }

                stepTwo();
            }
            else if (finish[i] == false)
                // Step 4: Print which processes are in a deadlock state
                // Print using P0, P1, ... , Pn format
                System.out.println("P" + i + " is in a deadlock state.");                           
        }
    }

    public static void main(String[] args) throws FileNotFoundException
    {

        AssignmentIII assignment; 

        String p_filepath;
        int p_numProcesses;
        int p_numResources;

        p_filepath = "inpu1t.txt";

        Scanner scan = new Scanner(System.in);
        System.out.println("Please enter the total number of processes: ");
        p_numProcesses = scan.nextInt();
        System.out.println("Please enter the number of different types of resources: ");
        p_numResources = scan.nextInt();
        scan.close();

        assignment = new AssignmentIII(p_numResources, p_numProcesses, p_filepath);

        try
        {
            assignment.initilizeMatrix();
        }
        catch(FileNotFoundException ex)
        {
            System.out.println("An error has occured. The file cannot be found.");
        }

        assignment.stepTwo();

    }

}
杜联
2023-03-14

像您一样声明数组-“在main之上”,但在以适当的大小读取后初始化它。

宣言:

public static int[] available;

初始化:

available = new int[numResources];
 类似资料:
  • 我有很多表,但它们有共同的自动增量。例如,我创建了我的第一个用户,它的id为1,然后我创建了一条消息,它的id为2,然后我创建了一条对消息的注释,它的id为3,但它的id应该为1,因为我没有任何其他注释或消息 我的用户: 我的信息: 我的评论是: 我将感谢任何帮助。

  • 我已经开发了一些代码,并且我的所有方法都使用了我创建的名为Account的自定义对象。我现在把帐户分成两种不同的帐户,存款帐户和储蓄帐户。我的问题是,我的所有方法都使用原始的account对象,因此,我将不得不为每个不同类型的account使用两次相同的方法。这看起来效率很低,改变起来有点繁琐。理想情况下,我希望接受用户输入saving/destine,然后创建相关对象并重复使用,这样我就不需要定

  • 由于我正在上传一个图像,我希望这是“图像/JPEG”或“图像/PNG”。有办法改变元数据吗? 更新:我之所以这么问,是因为如果一个人创建了一个应用程序,就像Instagram一样,提供了数千张图片。为每个正在获取的图像请求下载链接是不必要的,因为问题中的图像应该已经在默认情况下对公众开放。Firebase文件上传防止了这种情况,因此每个用户必须对每个图像提出一个请求(每次应用程序启动时可能会有成吨

  • 我需要处理从带有注释的类的公共方法中抛出的所有异常。我尝试使用Spring AOP。这是我的记录器: 是我的注释。 然后,我将注释添加到我的配置类中。 首先,我尝试注释一些引发异常的方法。它工作得很好,但是我如何才能使这种工作用于用注释注释的类中的所有公共方法呢?

  • 我有一节课: 在正常使用中,该类的行为与您预期的一样。方法和获取并设置复合列表。然而,我使用这个类作为一个对象,在使用JAX-WS构建的web服务中传递。当JAX-WS编译器看到这个类时,它会忽略和访问器,XSD中出现的唯一属性是。 在一天的大部分时间里,我的头撞在墙上,我决定尝试将私有方法的名称改为,突然间一切都如你所料。JAX-WS为属性创建了正确的模式。 似乎正在发生的事情是,JAX-WS看