【发布时间】:2019-04-18 04:28:13
【问题描述】:
所以我试图打印出从用户输入创建的数组内容(大小必须是奇数,并且在 3 到 11 之间),它兼作数组的列和行。在某些地方用字符来制作图案。一切都很好,除了格式。它打印正确,但不在正确的位置。由于某种原因,字符已关闭,连字符以错误的顺序打印。它们应该在阵列之前和之后打印。连字符的数量是正确的,只是我应该得到
-----------
*
*
*
*
*
-----------
但我得到了
*
*
*
*
*
-----------
-----------
我不知道为什么另一条连字符线离得这么远,这几乎是喜剧性的。这是代码
public static void main (String [] args) {
// instance variables - replace the example below with your own
int dimension = findDimension();
char [] [] array2d = new char [dimension] [dimension];
char star = '*';
array2d = leftDiagonal(star, dimension);
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( int row = 0; row < arrayParam.length; row++) {
for (int column = 0; column < arrayParam.length; column++) {
System.out.print(" " + arrayParam[row][column]);
}
}
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) {
System.out.print(starParam);
}
else {
System.out.print(' ');
}
}
System.out.println();
}
return leftD;
}
更新:在考虑了我被告知的内容后,我设法获得了正确的代码。在这里,再次感谢大家的帮助。
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;
}
【问题讨论】:
-
你想做什么,你可以尝试任何你想要的代码?或者你必须调试这个特定的代码?
-
只要格式正确,我可以尝试任何我想要的代码,但只要我能理解我的错误最终是我最想要实现的。我不明白为什么输出如此关闭。
-
你是如何运行这段代码的?您只填充“左对角线”,并且在您预期的“输出”中,您在所有数组中添加了
*? -
目前我只运行“leftDiagonal”是的。我在“leftDiagonal”中填充了数组,但仅针对行和列都匹配的区域,其他区域有一个空白区域。这就是我获得所需模式的方式。我只是不确定为什么它不能以正确的格式打印。
-
我正在回答它......一切正常
标签: java arrays loops format output