java - assign value to int[] array in do-while loop -


coming ruby/rails have lots of difficulties adopting java logic.

question - how assign values (read stdin) arrays of integers in do-while loop use arrays later in later methods?

here code snippet:

void triangleperimeter() {         //int[][] arrs;         //int[] edge1, edge2, edge3 = new int[];         {             system.out.print("x, y edge 1: ");             int[] edge1 = {ch3ex.stdin.nextint(),ch3ex.stdin.nextint()};             system.out.print("x, y edge 2: ");             int[] edge2 = {ch3ex.stdin.nextint(),ch3ex.stdin.nextint()};             system.out.print("x, y edge 3: ");             int[] edge3 = {ch3ex.stdin.nextint(),ch3ex.stdin.nextint()};             int[][] arrs = {edge1,edge2,edge3};              if (!(trianglehelpers.inputvaliditychecker(arrs)))                 system.out.println("wrong input. retype.");           } while(!(trianglehelpers.inputvaliditychecker(arrs)));          triangle triangle = new triangle();         triangle.computesides(arrs);          system.out.printf("triangle perimeter == %.2f\n", triangle.perimeter());     } 

==================

ch3ex.stdin.nextint(); - scanner object reads system.in

==================

console errors:

tasks3.java:209: error: cannot find symbol         } while(!(trianglehelpers.inputvaliditychecker(arrs)));                                                        ^   symbol:   variable arrs   location: class tasks3 tasks3.java:212: error: cannot find symbol         triangle.computesides(arrs);                               ^   symbol:   variable arrs   location: class tasks3 2 errors 

if do:

  • without do-while, works perfectly;

  • declare arrays above do-while , remove declarations in do-while leaving:

    edge1 = {ch3ex.stdin.nextint(),ch3ex.stdin.nextint()};

then tasks3.java:199: error: illegal start of expression edge1 = {ch3ex.stdin.nextint(),ch3ex.stdin.nextint()};

  • declaration , memory assign outside do-while:

    int[] side1 = new int[2];

and inside do-while assign values separately:

side[0] = ch3ex.stdin.nextint(); 

everything works perfectly.

========

where problem understanding logic of arrays + loops

the solution is:

declare

int[][] arrs; int[] edge1, edge2, edge3; 

outside do-while loop.

inside do-while loop following:

edge1 = new int[]{ch3ex.stdin.nextint(),ch3ex.stdin.nextint()}; 

for every 1-dim array and

arrs = new int[][]{edge1,edge2,edge3}; 

for 2-dim array.


Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -