Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在数组显示中跳过行_Java_Arrays - Fatal编程技术网

Java 在数组显示中跳过行

Java 在数组显示中跳过行,java,arrays,Java,Arrays,该程序显示一组n行和n列;其中,n由用户输入。显示器应该是一个nbyn数组,但是在这个程序中,它只是在一行中显示它们 例如,如果用户输入3,则输出应为 1 2 3 4 5 6 7 8 9 相反,它显示的是 1 2 3 4 5 6 7 8 9 有人能帮我吗?谢谢 public class Question2 { public static void main(String[] args) { //Title System.out.println("[--------------

该程序显示一组
n
行和
n
列;其中,
n
由用户输入。显示器应该是一个
n
by
n
数组,但是在这个程序中,它只是在一行中显示它们

例如,如果用户输入
3
,则输出应为

1 2 3
4 5 6
7 8 9
相反,它显示的是

1 2 3 4 5 6 7 8 9
有人能帮我吗?谢谢

public class Question2 {

  public static void main(String[] args) {

//Title
    System.out.println("[----------------------]");
    System.out.println("[     Array Pattern    ]");
    System.out.println("[----------------------]");
    System.out.println("");

//declare scanner
    Scanner keyboard = new Scanner (System.in);

//Prompt user to enter a digit greater than or equal to 3
    System.out.println("How many rows/columns do you want your array to have? (Must be at least 3):");

//read user input
    int num = keyboard.nextInt();

//place constraints on int num so that if it is less than 3, the program does not execute
    while(num<3 )
    {
        System.out.println("Lets's try this again....");
        System.out.println("How many rows/colums do you want your array to have? (Must be at least 3):");
        num = keyboard.nextInt();   
    }

    //2D array with number of rows and columns entered by user
    int[][] array = new int [num][num];
    int inc=1;
    for(int i=0;i<array.length;i++)
        for(int j=0;j<array.length;j++)
    {
    array[i][j]=inc;
    inc++;

    }


    //replace all square brackets in array display 
    String a = Arrays.deepToString(array);
    a = a.replace("[", "").replaceAll("]","");

    //replace all commas in array display
    a = a.replace(",", "").replaceAll(",","");
    System.out.println(a);

  }

}
公开课问题2{
公共静态void main(字符串[]args){
//头衔
System.out.println(“[-------------------------]”);
System.out.println(“[Array Pattern]”);
System.out.println(“[-------------------------]”);
System.out.println(“”);
//申报扫描器
扫描仪键盘=新扫描仪(System.in);
//提示用户输入大于或等于3的数字
System.out.println(“您希望数组包含多少行/列?(必须至少为3):”;
//读取用户输入
int num=keyboard.nextInt();
//在int num上放置约束,以便在int num小于3时,程序不会执行

while(num不要将数组转换为字符串;这只是需要做更多的工作。相反,循环遍历元素,打印每个元素,然后在每个第三个元素后添加换行符,如下所示:

for (int i = 0; i < array.length; ++i) {
    print(i);   // print the number *without* a line break after it

    if (i % 3 == 0) {
        println();    // add a line break after every third element
    } else {
        print(" ");
    }
}

println();
for(int i=0;i
您不需要
数组。deepToString

相反,使用嵌套的
for
循环,就像执行
inc++
的代码一样

主要思想如下:

foreach row in array:
    print all values
    print a linebreak, via `System.out.println()`
要打印一行中的所有值,您可以使用另一个
for
循环。在该循环中,您可以使用
System.out.printf(“%5d”,value)
打印一个数字,并用足够的空格将其环绕,以便输出看起来格式良好


有关更多提示,请参阅Java乘法表的Rosetta代码示例。

使用循环显示所需内容。不要使用Arrays.deepToString,然后尝试根据需要调整输出。尝试在String a=Arrays.deepToString(array)之后添加a=a.replace(“],[”,“\n”)
output@CheeseCracker使用循环而不是
deepToString
。您将对输出有更多的控制。我刚刚测试了这一点,它可以
String a=Arrays.deepToString(arr).replace(“],[”,“\n”).replaceAll(“[\[\],]”);System.out.println(a)
但正如@Shashwat所说,应该使用循环,如果使用
字符串
数组进行尝试,这是非常不可靠的。