java矩阵0';s和1';s

java矩阵0';s和1';s,java,matrix,methods,Java,Matrix,Methods,嘿,伙计们,这是我的家庭作业问题:编写一个方法,使用以下标题在对话框中显示n×n矩阵: 公共静态无效打印矩阵(int n) 矩阵中的每个元素都是0或1,这是随机生成的。 3×3矩阵可能如下所示: 0110 0 0 0 11 到目前为止,我可以很容易地在扫描仪上打印出我的问题,但我不知道如何在对话框中完成 目前,我得到了一个错误: 错误:此处不允许使用“void”类型JOptionPane.showMessageDialog(null,printMatrix(n)); 1个错误 我知道它是一个vo

嘿,伙计们,这是我的家庭作业问题:编写一个方法,使用以下标题在对话框中显示n×n矩阵: 公共静态无效打印矩阵(int n)

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

0110

0 0 0

11

到目前为止,我可以很容易地在扫描仪上打印出我的问题,但我不知道如何在对话框中完成

目前,我得到了一个错误: 错误:此处不允许使用“void”类型JOptionPane.showMessageDialog(null,printMatrix(n)); 1个错误

我知道它是一个void,不能返回,但是,我的赋值要求该方法是void。我真正的问题是如何在方法中打印它?我已经研究这个问题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)+ " ");
            }
        }

    }
}
import java.util.*;
导入java.lang.*;
导入java.io.*;
导入javax.swing.JOptionPane;
/*只有当类是公共的时,类的名称才必须是“Main”*/
表意文字
{
//主要方法
公共静态void main(字符串[]args)
{
//提示用户输入数字
String stringInteger=JOptionPane.showInputDialog(null,“输入整数n以确定矩阵大小:”,“矩阵输入大小”,JOptionPane.INFORMATION\u消息);
//将字符串转换为整数
int n=Integer.parseInt(stringInteger);
showMessageDialog(null,printMatrix(n));
}   
//相应地生成并显示随机0和1
公共静态无效打印矩阵(int n)
{
//行取决于n次
对于(int行=0;行
我想你被要求
打印
。另外,我更喜欢生成角色。循环并调用
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);
}
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
方法的结果。

我想您被要求
打印
。另外,我更喜欢生成角色。循环并调用
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);
}
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
方法的结果。

此代码将在printMatrix()中执行所有操作

类对话框打印
{
公共静态void main(字符串[]args)
{
//提示用户输入数字
String stringInteger=JOptionPane.showInputDialog(null,“输入整数n以确定矩阵大小:”,“矩阵输入大小”,JOptionPane.INFORMATION\u消息);
//将字符串转换为整数
int n=Integer.parseInt(stringInteger);
打印矩阵(n);
}   
//相应地生成并显示随机0和1
公共静态无效打印矩阵(int n)
{
//行取决于n次
串某人“”;
对于(int行=0;行

此代码将完成printMatrix()中的所有操作

类对话框打印
{
公共静态void main(字符串[]args)
{
//提示用户输入数字
String stringInteger=JOptionPane.showInputDialog(null,“输入整数n以确定矩阵大小:”,“矩阵输入大小”,JOptionPane.INFORMATION\u消息);
//将字符串转换为整数
int n=Integer.parseInt(stringInteger);
打印矩阵(n);
}   
//相应地生成并显示随机0和1
公共静态无效打印矩阵(int n)
{
//行取决于n次
串某人“”;
对于(int行=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;
    }
私有静态对象打印矩阵(int n){
//列取决于n次
字符串随机数n[][]=新字符串[n][n];

对于(int row=0;row对您的方法所做的轻微修改,我提供了输出的屏幕截图

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;
    }
私有静态对象打印矩阵(int n){
//列取决于n次
字符串随机数n[][]=新字符串[n][n];

对于(int行=0;已忘记包含我的代码:请考虑将代码添加到您的问题中)。您必须调用方法>代码> PrimTMatRx()中的消息对话框。只要你不允许改变方法签名,你的方法的响应类型是无效的,所以你不能将该方法作为参数传递。请不要在异地资源上链接到你的代码,在问题本身中包含你的代码。我已经在这里粘贴了它。忘了包括我的代码:请考虑将代码添加到你的代码中。问题。只要不允许更改方法签名,您必须调用方法
printMatrix()
中的消息对话框响应