#What's the reason for these errors?

1 messages · Page 1 of 1 (latest)

silver zealot
#

public static void addition()
{
int user_int = 1;
Random rand = new Random();
Scanner scan = new Scanner(System.in);

    ``if (user_int == 1)``
    {
    ``int num1 = (int) (Math.random() * 10);``
    ``int num2 = (int) (Math.random() * 20);``
    
    
    ``System.out.println("What is " +num1+ "+" +num2+ "?");``
    ``int answer = scan.nextInt();``
    }
    
        ``else if (answer == num1 + num2) //error is occurs under "answer, num1, num2"``
        {
        
        }

    ``else`` 
    {
        ``System.out.print("Try again: ");``
        ``scan.nextInt();``
    }
    
}
trail agateBOT
#

<@&987246399047479336> please have a look, thanks.

trail agateBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

silver zealot
#

is it because I don't have any parameters in my method?

molten shadow
#

ur only telling us where it is, but not what it says

#

or can u share a screenshot perhaps?

silver zealot
#

a little confuse why it doesn't work

molten shadow
#

variables are only known inside the scope they have been created in

#

scope = {...}

#

after reaching the }, they are all killed

#

do u see how u created them inside this branch

#

they are not known outside the branch anymore

#

they are all dead

#

u probably didnt intend to make an else if but likely only if

#

hard to guess though

silver zealot
#

can I just move the bracket then?

molten shadow
#

depends, that would change the entire logic and flow

#

ultimately, u have to understand ur own code better and adjust it to make what u want

#

since i dont know what u want to do, i cant really help u like that

silver zealot