简体   繁体   中英

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. 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. (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.

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. Then you can use it in any program you like, such as a simple class with a main method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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