所以我试图在某些地方填充一个带有字符*的数组以获得模式。数组的大小(行和列)是相同的,由用户输入决定。必须是奇数并且在3到11之间,所以例如如果它们放入5则它产生5乘5阵列。无论如何,我试图扭转我得到的输出
-----------
*
*
*
*
*
----------- to get
-----------
*
*
*
*
*
----------- but instead I get
-----------
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
----------- I used 5 as the size example here in case that helps
问题是我的循环似乎没有正常工作,至少我认为。这是代码
public static void main (String [] args) {
int dimension = findDimension();
char [] [] array2d = new char [dimension] [dimension];
char star = '*';
array2d = leftDiagonal(star, dimension);
print(array2d);
array2d = rightDiagonal(star, dimension);
System.out.println();
print(array2d);
}
public static int findDimension() {
int dimension = 0;
Scanner keybd = new Scanner(System.in);
do {
System.out.print("Enter an odd integer between 3 and 11 please: ");
dimension = keybd.nextInt();
} while (dimension%2 == 0);
return dimension;
}
public static void print(char [] [] arrayParam) {
for (int hyphen = 0; hyphen < (arrayParam.length*2)+1; hyphen++) {
System.out.print("-");
}
System.out.println();
for(char[] row : arrayParam)
{
for(char c : row)
System.out.print(" " + c);
System.out.printf("\n");
}
for (int hyphen = 0; hyphen < (arrayParam.length*2)+1; hyphen++) {
System.out.print("-");
}
}
public static char [] [] leftDiagonal(char starParam, int dimenParam) {
char [] [] leftD = new char [dimenParam] [dimenParam];
for (int i = 0; i < dimenParam; i++){
for (int j = 0; j < dimenParam; j++) {
if (i == j)
leftD[i][j] = starParam;
else
leftD[i][j] = ' ';
}
}
return leftD;
}
我认为问题在这里是特别的,因为它决定了数组中保存了什么
public static char [] [] rightDiagonal(char starParam, int dimenParam) {
char [] [] rightD = new char [dimenParam] [dimenParam];
for (int i = 0; i < dimenParam; i++){
for (int j = 0; j < dimenParam; j++) {
rightD[i][j] = ' ';
// I fill all the element spaces with blanks first then put in the *
// If there's another way to do It I'd like to know
}
}
for (int i = 0; i < dimenParam; i++){
for (int j = rightD.length-1; j >= 0; j--) {
rightD[i][j] = starParam;
}
}
return rightD;
}
将您的rightDiagonal
方法更改为
public static char [] [] rightDiagonal(char starParam, int dimenParam) {
char [] [] rightD = new char [dimenParam] [dimenParam];
for (int i = 0; i < dimenParam; i++){
for (int j = 0; j < dimenParam; j++) {
rightD[i][j] = ' ';
// I fill all the element spaces with blanks first then put in the *
// If there's another way to do It I'd like to know
}
}
for (int i = 0; i < dimenParam; i++){
for (int j = rightD.length-1; j >= 0; j--) {
if(i + j == rightD.length - 1) {
rightD[i][j] = starParam;
} else {
rightD[i][j] = ' ';
}
}
}
return rightD;
}
这里最重要的部分是条件:
if(i + j == rightD.length - 1) {
rightD[i][j] = starParam;
}
根据我的理解,您希望在垂直中间分割阵列,然后在另一侧镜像它:
* |
* |
* |
* |
*|
至
*|
* |
* |
* |
* |
因此,您需要将数组的宽度除以2,然后从新宽度中减去*中的索引
public static void swap_array() {
int x = your_size = 4; // 4 because thats a lot easier to understad than 5
String your_array[][] = new String[x][x];
for(int i = 0; i < your_array.length; i++) {
your_array[i][i] = "*";
}
// Now you have your start array
int new_length = your_array.length / 2; // For the mid
for(int j = 0; j < your_array.length; j++) { // Because of the 2 Dimensions
for(int k = 0; k < your_array.length; k++) {
if(your_array.equals("*")) {
your_array[j][k] = your_array[j][k - new_length];
}
break; // Because you only got one *, so we can cut the rest then
}
}
// Output
for(int l = 0; l < your_array.length; l++) {
for(int m = 0; m < your_array.length; m++) {
System.out.println(your_array[l][m]);
}
}
}
//这还没有在IDE中测试过。它是由大脑动态完成的。不要复制粘贴
你的rightDiagonal
方法应该类似于你的leftDiagonal
方法。唯一的区别不是检查i==j
而是i==length-j
public static char [] [] rightDiagonal(char starParam, int dimenParam) {
char [] [] rightD = new char [dimenParam] [dimenParam];
for (int i = 0; i < dimenParam; i++){
for (int j = 0; j < dimenParam; j++) {
if (i == (dimenParam-1-j) )
rightD[i][j] = starParam;
else
rightD[i][j] = ' ';
}
}
return rightD;
}