簡體   English   中英

java 在單獨的文件中將二維數組傳遞給另一個 class

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

我正在嘗試從 MatrixMaker 獲取 Matrix 數組以在驗證中訪問,但創建 object 和使用擴展不會解決問題。 這些文件位於兩個單獨的文件中,但我認為這不會阻止我訪問它。

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();
        
        }

     }
      
     
}

驗證是為了讓代碼可以運行檢查以查看矩陣是否為 n-1。 (所有行的總和等於 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);
}
}

您正在處理兩個類,它們都是沒有對象的主要方法。 您需要讓 MatrixMaker 成為 Java object。 然后你可以在任何你喜歡的程序中使用它,比如一個簡單的 class 和一個 main 方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM