gpt4 book ai didi

java - 方法以离线方式打印数组元素,并且连字符乱序

转载 作者:行者123 更新时间:2023-11-30 05:40:33 24 4
gpt4 key购买 nike

因此,我尝试打印根据用户输入创建的数组内容(大小必须为奇数且在 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;
}

最佳答案

您的代码中有几个问题,我已修复它们并在代码中提到它们:

 import java.util.Scanner;
public class PrintShape
{
public static void main (String [] args)
{
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)
{
// i cant understand why are you printing so many hyphen "(arrayParam.length*2)+1"
// so i left it on you
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 ");
}

//Problem:
// this "-" starts where the array printing end as you are not printing any newline ..
// it starts printing hyphen on the same line.. that why you get the second row of "-" so far

//Fixed:
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)
{
// Probelm : you are just printing the "*"and no saving it in the array
// thats why you are getting only blank spaces in the "print()"
System.out.print(starParam);
leftD[i][j] = starParam;
}
else
System.out.print(' ');
// soution : save it in the array

}
System.out.println();
}
return leftD;
}
}

如果您发现困难,请告诉我。

关于java - 方法以离线方式打印数组元素,并且连字符乱序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55739159/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com