Scanning for input without storing the values in a variable

后端 未结 2 1539
感情败类
感情败类 2021-01-28 12:01

I\'m declaring a 2d Array with 100 rows and columns. Im trying to get the user to dictate the numbers that go into the array. Im supposed to store the values without storing the

相关标签:
2条回答
  • 2021-01-28 12:22

    Well, first of all, you are dealing with a two dimensional array, so you will need two loops, one for the rows and the other for the colums.

    for(int i=0; i<100; i++)
     {
         for(int j=0;j<100;j++)
            {
                   nums[i][j] = scan.nextInt();
            }
    }
    

    This syntax - int[scan.nextInt()][scan.nextInt()]; is not even legal.

    0 讨论(0)
  • 2021-01-28 12:31

    You'll need to use nested for loops for a 2-d array (one for rows and one for columns):

    for (int i = 0; i < nums.length; ++i)
         for (int j = 0; j < nums[i].length; ++j)
    {
    
        nums[i][j] = scan.nextInt();
    
    }
    
    0 讨论(0)
提交回复
热议问题