Java 我的简单2D数组的问题
因此,我尝试使用netbeans 8.1在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
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的区别吗?哦!非常感谢你!我没注意到。一定是出于习惯,哈哈。如果你的问题被解决了,请将答案标记为已解决。