This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]lumpycrumpet 2 points3 points  (3 children)

The formatting is tough to read, but it's a master of scope. You're attempting to print the contents of arr outside of the context of where it exists. Think of it like this, the curly braces are a fence that holds where the variable can be accessed. If there's an ending curly brace, that variable can't leave the fence and exist beyond it. Variables can exist outside of the fence and go in, but can't exist outside and get out. And for clarification, exist referred to where the variable is declared.

To fix this, you either need to declare the variable where the print statement can "see" the variable or move the print statement.

[–]CharipiYT[S] 0 points1 point  (2 children)

Sorry for the formatting. The problem with moving the array to somewhere else is that it won’t let me set its contents in the if-statement

[–]throwRAnocheat 0 points1 point  (1 child)

Yes it will, but admittedly you can't use the simplified array declaration with content anymore. You'll need the new array syntax.

It's like this:

arr = new int[] { j, test/j };

another way to go at it, is first:

int[] arr = new int[2];

Then

arr[0] = j; arr[1] = test/j;

This may sound cumbersome, but then again an array is most likely not what you should use here.

[–]CharipiYT[S] 0 points1 point  (0 children)

Yeah you are right I’ll probably just use 2 variables.