Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.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

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 我的简单2D数组的问题_Java_Arrays - Fatal编程技术网

Java 我的简单2D数组的问题

Java 我的简单2D数组的问题,java,arrays,Java,Arrays,因此,我尝试使用netbeans 8.1在Java中创建一个基本的2d数组,其中包含行和列 这是我的代码: public static void main(String[]args) { int temp = 5; int temp2 = 10; for(int i = 0; i < temp2; ++i) { for(int k = 0; k < temp; ++k) { System.out

因此,我尝试使用netbeans 8.1在Java中创建一个基本的2d数组,其中包含行和列

这是我的代码:

public static void main(String[]args)
{
    int temp = 5;
    int temp2 = 10;

    for(int i = 0; i < temp2; ++i)
    {
        for(int k = 0; k < temp; ++k)
        {
            System.out.println("|_|");
        }
        System.out.println("\n");
    }
}
publicstaticvoidmain(字符串[]args)
{
内部温度=5;
int temp2=10;
对于(int i=0;i
但出于某种原因,输出如下所示:


有人能帮我理解出什么问题吗?

看来您应该将
print
println
结合使用,如下所示:

public static void main(String[]args)
{
    int temp = 5;
    int temp2 = 10;

    for(int i = 0; i < temp2; ++i)
    {
        for(int k = 0; k < temp; ++k)
        {
            System.out.print("|_|"); //Prints each cell one after another in the same row.
        }
        System.out.println(""); //Prints a new row, .println("\n") will print two new rows.
    }
}
publicstaticvoidmain(字符串[]args)
{
内部温度=5;
int temp2=10;
对于(int i=0;i
我发现足以解决您的问题,但是
System.out.println(“”)让我有点恼火。
所以,我会这样做:

public static void main(String[] args) {
    int temp = 5;
    int temp2 = 10;

    for(int i = 0; i < temp2; ++i)
    {
        StringBuilder row = new StringBuilder();
        for(int k = 0; k < temp; ++k)
        {
            row.append("|_|");  //concat the cells in the same String
        }
        System.out.println(row.toString()); //prints one entire row at a time
    }
}
publicstaticvoidmain(字符串[]args){
内部温度=5;
int temp2=10;
对于(int i=0;i
你知道println和print的区别吗?哦!非常感谢你!我没注意到。一定是出于习惯,哈哈。如果你的问题被解决了,请将答案标记为已解决。