Multi Dimentional Arrays Java question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Multi Dimentional Arrays Java question

I don't know how to make this code work properly, I have a 2d array and have to check if the seat is available or is taken and mark as Free or Sold, first the loop keep running and second the check sometimes works and mark properly and sometimes even if a seat is free mask as sold. public class Main { public static void main(String[] args) { Locale.setDefault(Locale.US); Scanner sc = new Scanner(System.in); int[][] seats = { { 0, 0, 0, 1, 1, 1, 0, 0, 1, 1 }, { 1, 1, 0, 1, 0, 1, 1, 0, 0, 0 }, { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, { 0, 0, 0, 1, 1, 1, 1, 0, 0, 0 }, { 0, 1, 1, 1, 0, 0, 0, 1, 1, 1 }, }; int roomRow = sc.nextInt(); int roomColumn = sc.nextInt(); for (int row = 0; row < seats.length; row++) { for (int column = 0; column < seats[row].length; column++) { if(row == roomRow && column == roomColumn) { System.out.println("Free"); } else { System.out.println("Sold"); } } } sc.close(); } } this is my code if anyone can help I'll be glad

19th Sep 2023, 10:56 AM
Pedro Simões
Pedro Simões - avatar
1 Answer
+ 5
you only check if row == roomRow, etc. but not if there is 0 or 1 in the cell. Also, you don't need a loop. roomRow and roomColumn are just indices.
19th Sep 2023, 11:17 AM
Lisa
Lisa - avatar