2016-10-20 60 views
-1

我正在进行测试准备程序。它不是家庭作业或一个年级,我只需要帮助完成和修复它,这样我就可以学习并更好地理解它是如何工作的。方向是“编写一个名为Matrix1.java的程序,它随机地将0和1填充到一个n×n矩阵中,打印矩阵。”我对编码仍然很陌生,所以任何帮助将不胜感激。这是我到目前为止的代码:JAVA需要涉及矩阵和阵列的帮助

public class Matrix1{ 

    public static void main(String[] args){ 
     Matrix1 matrix=new Matrix1(5); 
     matrix.fill(); 
     matrix.print(); 
    } 

    public Matrix1(int n){ 
     int[][] matrix = new int[n][n]; 
    } 
    public void fill(int n){ // randomly fill in 0s and 1s 

     Random rand = new Random(); 
     for(int i = 0; i < n; i++){ 
      for(int j = 0; j < n; j++){ 
       Integer r = rand.nextInt; 
       matrix[i][j] = Math.abs(r); 
      } 
     } 
    } 
    public void print(int[][]matrix, int n){ //print the matrix, each row is printed in a separate line 
     for(int i = 0; i< n; i++){ 
     for(int j = 0; j<n; j++){ 
      System.out.println(array[i][j]); 
     } 
     } 
    } 
} 

我最终迷惑了自己,我不确定如何解决或继续。尽管如此,我认为我在正确的轨道上。

+3

阅读编译器给你的信息。如果你不了解它们,请将它们输入到你最喜欢的搜索引擎中。 –

+1

有人提出这个问题吗?这里没有*特定的*问题。 –

回答

0

你的修改后的代码。

import java.util.Random; 

public class Matrix1 { 

private final int[][] matrix; 
private final int n; 

public Matrix1(int n) { 
    this.n = n; 
    this.matrix = new int[n][n]; 
} 

/** 
* randomly fill in 0s and 1s 
*/ 
public void fill() { 
    Random rand = new Random(); 
    for (int i = 0; i < n; i++) { 
     for (int j = 0; j < n; j++) { 
      matrix[i][j] = rand.nextInt(2); 
     } 
    } 
} 

/** 
* print the matrix, each row is printed in a separate line 
*/ 
public void print() { 
    for (int i = 0; i < n; i++) { 
     for (int j = 0; j < n; j++) { 
      System.out.print(matrix[i][j]); 
     } 
     System.out.println(); 
    } 
} 

public static void main(String[] args) { 
    Matrix1 matrix = new Matrix1(5); 
    matrix.fill(); 
    matrix.print(); 
} 

} 

因此改变我做:

  • 我已经改变了“矩阵”和“n”变量分为类领域,所以你不必通过类的方法来传递他们
  • 填充数组 - 它应该只是0/1而不是任何整数,因此您可以使用rand.nextInt(2)和边界 - 它给出的值小于2
  • 打印数组 - 您必须在一行中打印行,然后更改
+4

你能解释一下你的改变,而不是仅仅在这里抛弃代码吗? – Robert

0

它看起来像正确的方向,检查你的主要方法来设置必要的参数(大小,数组和大小)。

还设置随机对象只有0或1(检查Java类库的答案)我相信你可以在Random.nextInt方法内设置一个参数。

此外,如果需要打印它像一个双阵列中,改变了您的打印逻辑,因为你总是写入新行

0

好了,我看到了一些问题,而如果固定可能有助于在得到你正确的轨道。

首先,对于随机整数,你可能会想从this answer使用方法:

import java.util.concurrent.ThreadLocalRandom; 

// nextInt is normally exclusive of the top value, 
// so add 1 to make it inclusive 
ThreadLocalRandom.current().nextInt(min, max + 1); 

我不认为你需要在这种情况下使用Math.abs,只要你请致电ThreadLocalRandom.current().nextInt(0, 2)

其次,您的打印代码有几个问题。首先,你应该使用matrix[i][j]而不是array[i][j]。其次,您需要使用System.out.print而不是System.out.println

应该是这样的:

for(int i = 0; i< n; i++){ 
    for(int j = 0; j<n; j++){ 
     System.out.print(matrix[i][j]); 
    } 
    System.out.println(); 
} 
+0

非常感谢大家!我得到了我所需要的,我用java文档来帮助我使用我的原始代码,并且我意识到我的问题的一部分是我从来没有改变它来匹配我现有的代码。现在我明白了它背后的逻辑,我只是​​要继续研究它直到我掌握它。谢谢你们! – Kevin

0
public class Matrix1{ 

public static void main(String[] args){ 
    Matrix1 matrix = new Matrix1(5); 
    matrix.fill(); 
    matrix.print(); 
} 

private int[][] m; //it is easier use a global variable 

public Matrix1(int n){ 
    m = new int[n][n]; 
} 

public void fill(){ // randomly fill in 0s and 1s 
    for(int i = 0; i < m.length; i++){ 
     for(int j = 0; j < m.length; j++){ 
     if (Math.random() > 0.5) { 
      m[i][j] = 1; 
     } else { 
      m[i][j] = 0; 
     } 
     } 
    } 
} 

public void print(){ //print the matrix, each row is printed in a separate line 
    for(int i = 0; i< m.length; i++){ 
    for(int j = 0; j<m.length; j++){ 
     System.out.print(m[i][j]); //only use print() for the same row 
    } 
    System.out.println(""); 
    } 
} 
}