当前位置: 首页 > 知识库问答 >
问题:

Java矩阵0和1

郜玉石
2023-03-14

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

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

0 1 0

0 0 0

1 1 1

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

此刻,我得到的错误:错误:无效类型不允许在这里JOptionPane.showMessageDialog(空,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个答案

太叔睿
2023-03-14

这段代码将在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);

        }
    }
闻人飞翼
2023-03-14

对你的方法做了一点修改,我提供了输出的屏幕截图。

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

希望你发现我的代码有帮助干杯快乐编码。

段坚
2023-03-14

我想你被要求打印。另外,我更喜欢Random。用于生成字符的nextBoolean()。循环并调用系统。出来打印。比如,

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

但是一个val方法不返回任何内容,因此您不能在调用方中打印一个val方法的结果。

 类似资料:
  • 我在查看一些代码时发现了以下内容: 有什么区别呢?顺便说一句:我对矩阵很陌生

  • 我目前正在做一个音频信号处理项目,需要在Java中的一个复杂矩阵上使用SVD。我当前的线性代数库是Apache Commons。但它只提供实矩阵的SVD,JAMA、JBLAS、EJML、ojAlgo都不支持复杂的SVD。 我一直在用一些技巧从一个等效的实矩阵中找到SVD。然而,当我重建矩阵时,这种技术对于虚部有很大的不准确性。

  • 本章节主要给大家引入视图矩阵和投影矩阵两个新的概念,如果你没有图形学基础,对这两个概念暂时还没有认知,也没有关系。通过前面的学习相信你对平移、旋转等矩阵有了一定的认知,至于投影和视图矩阵和平移、旋转等模型矩阵一样也会对WebGL顶点进行坐标变换,至于如何变换下面会逐步讲解。 在学习本章节之前,如果你对Three.js已经有了一定的了解,可以尝试从WebGL视图矩阵和投影矩阵的角度去深入理解Thre

  • 问题内容: 我有以下内容: 如何在XYZ_2上执行与在XYZ_2上相同的操作?我会以某种方式首先重塑数组吗? 问题答案: 您似乎正在尝试的最后一个轴 与最后一个 。因此,您可以像这样使用- 相关帖子了解。 为了完整起见,在交换的最后两个轴后,我们当然也可以使用,例如- 这将不如一个高效。 运行时测试- 一般而言,涉及张量时,效率要高得多。由于的轴只有一个,因此我们可以通过重整,使用,获取结果并将其

  • 着色器语言中通过关键字mat2、mat3、mat4分别声明一个2x2矩阵、3x3矩阵、4x4矩阵,通过内置函数mat2()、mat3()、mat4()分别创建一个2x2矩阵、3x3矩阵、4x4矩阵。 关键字 数据类型 mat2 2x2矩阵,4个元素 mat3 3x3矩阵,9个元素 mat4 4x4矩阵,16个元素 声明变量 关键子mat4声明一个4x4矩阵 mat4 matrix4; 构造函数赋

  • 本文向大家介绍Python中矩阵创建和矩阵运算方法,包括了Python中矩阵创建和矩阵运算方法的使用技巧和注意事项,需要的朋友参考一下 矩阵创建 1、from numpyimport *; a1=array([1,2,3]) a2=mat(a1) 矩阵与方块列表的区别如下: 2、data2=mat(ones((2,4))) 创建一个2*4的1矩阵,默认是浮点型的数据,如果需要时int类型,可以使用