二维数组数学序列 (Java)
2d array mathematical sequence (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("[\[\]]", ""));
}
所以我必须打印出一个数组:
- 第一个单元格定义方形数组的维度。所以,我有
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("[\[\]]", ""));
}