【问题标题】:Method prints array elements way off line, and hyphens out of order方法以离线方式打印数组元素,并且连字符乱序
【发布时间】: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


【解决方案1】:

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

 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;
    }
}

如果你觉得有什么困难,请告诉我。

【讨论】:

  • 谢谢,这似乎在大多数情况下都可以正常工作。我知道我现在必须将我的字符保存在数组中,但它现在打印出每个元素都填充了一个字符。
  • 你只想要对角线?
  • 首先在您的leftDiagonal() 中打印左对角线...在print() 中您再次只想打印对角线?
  • 因为根据您的预期输出,您应该打印所有具有“*”的数组
  • 天哪,你是对的。抱歉,我复制并粘贴了我想要的错误输出。我现在改变了它,但是我只想要对角线。我设法通过在 if 和 else 语句下方设置 leftD[i][j] = starParam 和 leftD[i][j] = ' ' 来接近它。现在唯一的问题是间距关闭了。
猜你喜欢
  • 2019-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-02
  • 2011-07-28
  • 2021-08-04
  • 1970-01-01
相关资源
最近更新 更多