Pergunta

Please keep in mind my switch statement is not complete yet. I'm trying to figure out why that when I try and print intArray everything comes out in a single column. 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
Foi útil?

Solução

There's a difference between System.out.println() and 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. I also added an additional call to println() between the rows so that they'll print out on their own lines.

Outras dicas

System.out.println(intArray[row][col]) - println will end every print with a new line. To avoid this, use system.out.print()

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top