java矩阵0和1

时间:2016-03-13 00:29:54

标签: java matrix methods

嘿伙计们这是我的作业问题:编写一个方法,使用以下标题在对话框中显示n×n矩阵: public static void printMatrix(int n)

矩阵中的每个元素都是0或1,它是随机生成的。 3乘3矩阵可能如下所示:

0 1 0

0 0 0

1 1 1

到目前为止,我可以轻松地在扫描仪中打印出我的问题,但是我不确定如何在对话框中执行此操作。

目前我收到错误: 错误:此处不允许使用'void'类型JOptionPane.showMessageDialog(null,printMatrix(n)); 1错误

我知道这是一个空白,但不能返回,但我的任务要求方法无效。我真正的问题是如何在方法中打印它?我一直在研究这个问题4个小时,这真让我感到沮丧。

import java.util.*;
import java.lang.*;
import java.io.*;
import javax.swing.JOptionPane;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    // Main method
    public static void main(String[] args)
    {
        // Prompt user to enter numbers
        String stringInteger = JOptionPane.showInputDialog(null, "Enter a integer n to determine the size of matrix: ", "Size of Matrix Input", JOptionPane.INFORMATION_MESSAGE);

        // Convert string to integer
        int n = Integer.parseInt(stringInteger);
        JOptionPane.showMessageDialog(null, printMatrix(n));
    }   

    // Generate and display random 0's and 1's accordingly
    public static void printMatrix(int n)
    {
        // Row depending on n times
        for (int row = 0; row < n; row++)
        {
            // Column depending on n times
            for (int col = 0; col < n; col++)
            {
                String randomN = ((int)(Math.random() * 2)+ " ");
            }
        }

    }
}

3 个答案:

答案 0 :(得分:0)

我认为你被要求print。另外,我更喜欢Random.nextBoolean()来生成角色。循环并致电System.out.print。像,

public static void printMatrix(int n) {
    Random rand = new Random();
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            System.out.print(rand.nextBoolean() ? "1 " : "0 ");
        }
        System.out.println();
    }
}

public static void main(String[] args) {
    printMatrix(3);
}

如果确实想要使用JOptionPane,您可以使用StringBuilder构建矩阵,然后显示它。像,

public static void printMatrix(int n) {
    Random rand = new Random();
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            sb.append(rand.nextBoolean() ? "1 " : "0 ");
        }
        sb.append(System.lineSeparator());
    }
    JOptionPane.showMessageDialog(null, sb.toString());
}

但是void方法不返回任何内容,因此您无法在调用者中打印void方法的结果。

答案 1 :(得分:0)

对您的方法进行了轻微修改我正在提供输出的屏幕截图。

private static Object printMatrix(int n) {
        // Column depending on n times
        String randomN[][] = new String[n][n];
        for(int row = 0 ;row<n;row++)
        {
              for (int col = 0; col < n; col++)
                 {
                      randomN[row][col] = ((int)(Math.random() * 2)+ " ");
                 }
        }
        String s = Arrays.deepToString(randomN).replace("], ", "\n").replaceAll(",|\\[|\\]", "");
        return s;
    }

image

希望您发现我的代码有助于欢呼编码。

答案 2 :(得分:-1)

此代码将执行printMatrix();

内的所有操作
   class DialogPrint
    {
        public static void main(String[] args)
        {
            // Prompt user to enter numbers
             String stringInteger = JOptionPane.showInputDialog(null, "Enter a integer n to determine the size of matrix: ", "Size of Matrix Input", JOptionPane.INFORMATION_MESSAGE);

            // Convert string to integer
            int n = Integer.parseInt(stringInteger);
            printMatrix(n);
        }   

        // Generate and display random 0's and 1's accordingly
        public static void printMatrix(int n)
        {
            // Row depending on n times



            String sb="";


            for (int row = 0; row < n; row++)
            {
                // Column depending on n times
                for (int col = 0; col < n; col++)
                {
                    String randomN = ((int)(Math.random() * 2)+ " ");
                    sb+=randomN;
                }
                sb+="\n";
            }

            System.out.print(sb);

            JOptionPane.showMessageDialog(null, sb);

        }
    }

enter image description here

相关问题