Sunday 15 September 2013

Seat Locator - 3D Arrays (Java) -



Seat Locator - 3D Arrays (Java) -

i got 3 dimensional array display, having problem getting user input correspond programme , find seat (replacing specific 0 1). please give me hint of suppose do? here code.

import java.util.scanner; public class ticketseller { //name public static void main(string[] args) { scanner keyboard = new scanner(system.in); int seating[][][] = new int[3][10][10]; int g,r,c; system.out.print("enter ticket section: "); g = keyboard.nextint(); system.out.print("enter ticket row: "); r = keyboard.nextint(); system.out.print("enter ticket number: "); c = keyboard.nextint(); for( g = 0; g<3; g++) for(r=0; r<10; r++){ for(c=0; c<10; c++) seating[g][r][c]= g*r*c; { for( g = 0; g<3; g++) for(r=0; r<10; r++){ for(c=0; c<10; c++) { system.out.print(seating[g][r][c]); } system.out.println(); } system.out.println(); } } }}

and output:

enter ticket section: 1 come in ticket row: 1 come in ticket number: 1 0000000000 0000000000 0000000000

first, should utilize ide , format code properly. proper indentation of import understanding of problems in code.

problem 1

you using same variable names 1 time again , again. utilize variables g,r , c loop variables, erase previous values, user entered. should utilize different variables loops.

problem 2

you have 2 nested loops (for g , for r). within for r have loop fills seatings values - c cells current g , r. since 0 @ first round, fills 0 in seatings.

then, after seatings filled, have 1 time again loops same loop variables. loops need - print seatings. seatings @ stage zero. , that's output get.

but because used the same loop variables, variables c,r , g have reached maximum values (3,10,10). means outer loop not performed again.

never utilize loops within loops same variables.

problem 3

this problem related previous problem. , proper indentation , formatting have helped you. part:

for( g = 0; g<3; g++) for(r=0; r<10; r++){ for(c=0; c<10; c++) seating[g][r][c]= g*r*c; {

both { here should not here. first one, right after for r causes sec set of loops included in first. sec 1 not creating loop block. thought closing block, opening new block.

these loops proper formatting:

(g = 0; g < 3; g++) (r = 0; r < 10; r++) { ┆ (c = 0; c < 10; c++) ┆ seating[g][r][c] = g * r * c; ┆ { ┆ ╻ (g = 0; g < 3; g++) ┆ ╻ (r = 0; r < 10; r++) { ┆ ╻ (c = 0; c < 10; c++) { ┆ ╻ system.out.print(seating[g][r][c]); ┆ ╻ } ┆ ╻ system.out.println(); ┆ ╻ } ┆ ╻ system.out.println(); ┆ } }

the part marked ┈┈┈ within for r loop. part marked ╺╺╺╺ independent block , it's under for r, not for c loop, because it's not connected () of for.

the solution always set { after for(). if set { after for, while, if , else, if there 1 line, you'll avoid lot of troubles because compiler tell if have missing brace, , if decide add together 1 of loops, won't commands thought within loop or if , outside it.

problem 4

there no reason set g*r*c within seats. wanted set 1 in appropriate seat, that.

in conclusion:

don't utilize same variable names user inputs , loop variables. phone call user variables usersection, userrow, usernumber , loop variables section, row , number example. take care have { after each for() , set } in proper place. you didn't set 1 in there anywhere.

java arrays

No comments:

Post a Comment