[英]2D Array printing in one column error
Please keep in mind my switch statement is not complete yet. 请记住,我的switch语句尚未完成。 I'm trying to figure out why that when I try and print intArray everything comes out in a single column. 我试图弄清楚为什么当我尝试打印intArray时所有内容都出现在单个列中。 If anyone needs details don't hesitate to ask. 如果有人需要详细信息,请随时询问。
import java.util.*;
public class twoDimensionalArray
{
public static void main(String[] args)
{
Scanner console = new Scanner(System.in);
int choice, row, col, upper, lower;
System.out.println("Choose the number of rows for the array");
row = console.nextInt();
System.out.println("Choose the number of columns for the array");
col = console.nextInt();
int[][] intArray = new int[row][col];
choice = Menu();
switch(choice)
{
case 1:
do
{
System.out.println("Choose a lower bound for the random numbers");
lower = console.nextInt();
System.out.println("Choose an upper bound for the random numbers");
upper = console.nextInt();
if (lower > upper)
{
System.out.println("The lower bound must be less than the upper bound");
}
}
while (lower > upper);
fillArray(intArray, upper, lower);
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10: System.exit(0);
}
}
public static int Menu()
{
Scanner console = new Scanner(System.in);
int choice;
do
{
System.out.println("Enter the number corresponding to your choice:\n"
+ "1) Fill the array with new values \n"
+ "2) Find largest element \n"
+ "3) Find smallest element \n"
+ "4) Find Sum of elements \n"
+ "5) Find average of elements \n"
+ "6) Count the number of even elements \n"
+ "7) Total the odd values \n"
+ "8) FindIt \n"
+ "9) Print Array \n"
+ "10) Exit");
choice = console.nextInt();
}
while (choice < 1 || choice > 10);
return choice;
}
public static int[][] fillArray(int[][] intArray, int upper, int lower)
{
for (int row = 0; row < intArray.length; row++)
{
for (int col = 0; col < intArray[row].length; col++)
{
intArray[row][col] = lower + (int)(Math.random() * ((upper - lower) + 1));
}
}
for (int row = 0; row < intArray.length; row++)
{
for (int col = 0; col < intArray[row].length; col++)
{
System.out.println(intArray[row][col]);
}
}
return intArray;
}//end fillArray
}//end program
There's a difference between System.out.println() and System.out.print(). System.out.println()和System.out.print()之间有区别。
Your code: 您的代码:
for (int row = 0; row < intArray.length; row++)
{
for (int col = 0; col < intArray[row].length; col++)
{
System.out.println(intArray[row][col]);
}
}
My code: 我的代码:
for (int row = 0; row < intArray.length; row++)
{
for (int col = 0; col < intArray[row].length; col++)
{
System.out.print(intArray[row][col]);
}
System.out.println("");
}
print()
prints the String argument, whereas println()
prints the argument followed by a new line. print()
打印String参数,而println()
打印参数,然后换行。 I also added an additional call to println()
between the rows so that they'll print out on their own lines. 我还在行之间添加了对println()
的附加调用,以便它们可以在自己的行上打印出来。
System.out.println(intArray[row][col]) - println will end every print with a new line. System.out.println(intArray [row] [col])-println将以新行结束每个打印。 To avoid this, use system.out.print() 为了避免这种情况,请使用system.out.print()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.