简体   繁体   English

java 在单独的文件中将二维数组传递给另一个 class

[英]java getting a 2d array to another class in a separate file

I'm trying to get the Matrix array from MatrixMaker to be accessible in Verifying, but the creation object and using extends won't rectify the issue.我正在尝试从 MatrixMaker 获取 Matrix 数组以在验证中访问,但创建 object 和使用扩展不会解决问题。 these are in two separate files but I wouldn't think that would be stopping me from accessing it.这些文件位于两个单独的文件中,但我认为这不会阻止我访问它。

import java.util.*;
import java.lang.*;
import java.util.Scanner;

public class MatrixMaker{

     public static void main(String []args){
         Scanner in = new Scanner(System.in);
        int inputcol = 0;
        int inputrow = 0;
        int newnum = 0;
        int uinput = 0;
        int repeat = 1;
        int[][] Matrix = new int[][]{
         { -10, 0, 0, 7, 3 },
         { 0, -9, 5, 4, 0 },
         {0, 8, -10, 2,  },
         { 1, 0, 0, -7, 6 },
         { 9, 0, 0, 0, -9 }
        }; 
        while(repeat!=0)
        {
        System.out.println(Arrays.deepToString(Matrix).replace("], ", "]\n").replace("[[", "[").replace("]]", "]"));
        System.out.println("The Matrix is 5X5 \n Select Option:\n 1 for View Value:\n 2 for Replace Value: ");
        uinput = in.nextInt();
        //int b[][]={{1,3,4},{3,4,5}}; 
        if(uinput==1)
        {
        System.out.println("Enter Row: ");
        inputrow = in.nextInt();
        System.out.println("Enter Cols:");
        inputcol = in.nextInt();
        System.out.println(Matrix[inputrow][inputcol]);
        }
        else
        if(uinput==2)
        {
            System.out.println("Enter Row: ");
        inputrow = in.nextInt();
        System.out.println("Enter Cols:");
        inputcol = in.nextInt();
        System.out.println("Enter New Number: ");
        newnum = in.nextInt();
        Matrix[inputrow][inputcol] = newnum;
        }
        else
        {
            System.out.println("Check your input. ");
        }
        System.out.println("Want to repeat it? if yes press 1\n for exit press 0 ");
        repeat = in.nextInt();
        
        }

     }
      
     
}

Verifying is so the code can run checks to see if the matrix is n-1 or not.验证是为了让代码可以运行检查以查看矩阵是否为 n-1。 (The sum of all row is equal to 0 For Markovian continuous-time descriptors: Only diagonal elements can be negative Usually some (several) elements can be zero) obviously not complete yet for all the checks. (所有行的总和等于 0 对于马尔可夫连续时间描述符:只有对角线元素可以是负数 通常一些(几个)元素可以为零)显然尚未完成所有检查。

import java.util.*;
import java.lang.*;
import java.util.Scanner;

public class Verifying extends MatrixMaker
{
 public static void main(String []args)
 {
 double sumRow;
      int count = 0;
      int negcount = 0;
      boolean diagonal = false;
      boolean square = false;
      boolean zeros = false;
      boolean order = false;
      
 
if(Matrix.length ==Matrix[0].length)//the matrix is a square
      {
         square = true;
      }

System.out.println(square);
}
}

You're dealing with two classes that are both just main methods with no objects.您正在处理两个类,它们都是没有对象的主要方法。 You need to have MatrixMaker be a Java object.您需要让 MatrixMaker 成为 Java object。 Then you can use it in any program you like, such as a simple class with a main method.然后你可以在任何你喜欢的程序中使用它,比如一个简单的 class 和一个 main 方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM