二维数组数学序列Java

如何解决二维数组数学序列Java

所以我必须打印出一个数组:

  • 一个单元格定义方形数组的维度。所以,我有 input Scanner
  • 在数组的第一行,每个单元格都必须是前一个单元格的三元组减一。
  • 对于下一行,每个单元格都必须具有上一行的同一列的双精度值加上该列的编号。
  • 例如:如果输入的维度是 4,比如 showArrray(creerArray(4));,那么它应该打印如下内容
    4 11 32 95
    
    8 23 66 193
    
    16 47 134 389

    32 95 270 781

我已经对输入维度的部分进行了编码,但我一直在试图弄清楚如何编码这个序列:

public static void showArray() {
    
}
 
public static void  createArray() {
    int square= 0;
    int[][] int2D = new int[square][square];

    java.util.Scanner input = new Scanner(system.in);
    square= input.nextInt();
    System.out.println("Enter the dimension of the array:" + square);
    int counter=0;
    for(int i=0; i<square; i++) {
        for(int j=0; j<square; j++){
            int2D[i][j]=counter;
            counter++;
        }
        input.close();
        
    }
    ****i have to start coding the sequence here
        
}

解决方法

Java please read here 中的数组一旦创建就无法更改。我建议你在创建数组之前先询问用户

    Scanner input = new Scanner(System.in);
    System.out.print("Enter the dimension of the array:"); // ask user
    int square= input.nextInt();  //accept user input here
    int[][] int2D = new int[square][square];  //and then create the array

编辑:您不能将“square”变量放入打印中,因为它尚未创建

,

关键在于您将计算放置在 for 循环中的哪个位置以生成二维数组,例如:

// Get Array size from User...
Scanner userInput = new Scanner(System.in);

int aryLength = 0; // To hold the square 2D Array Rows and Columns length.
while (aryLength == 0) {
    System.out.print("Enter 2D Array square size: --> ");
    String len = userInput.nextLine();
    /* Is the supplied value valid...
       The Regular Expression passed to the String#matches() method
       will return boolean true is the variable len only contains
       a string representation of a signed or unsigned integer 
       value.        */ 
    if (len.matches("-?\\d+")) {
        // Yes it is:  
        aryLength = Integer.valueOf(len);
    }
    else {
        System.err.println("Invalid numerical value (" + len + ") supplied!");
        System.err.println(); 
    }
}

// Declare and initialize the 2D Array
int[][] array = new int[aryLength][aryLength];

int startVal = aryLength; // Used to calculate the first cell of every array Row
// Iterate array Rows...
for (int i = 0; i < array.length; i++) {
    // Iterate Columns of each array row...
    int cellVal = startVal; // Used for the cell values for all columns in each row
    for (int j = 0; j < aryLength; j++) {
        array[i][j] = cellVal;
        cellVal = ((array[i][j] * 3) - 1); // calculate cell value
    }
    startVal = startVal * 2; // Calculate first cell value for very row.
}

// Display the 2D Array
System.out.println();
for (int i = 0; i < array.length; i++) {
    System.out.println(Arrays.toString(array[i]).replaceAll("[\\[\\]]",""));
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?