-1

Hey there I was wondering why the "return ask" here does not change the value of 'ask' when I print it out on my main method (it prints out 0 in the main but works in the Log method) and how I can fix it. Thanks in advance!

public static int Log(int ask){

    int b=0;
    int c =0;
    c = scannerobj.nextInt();
    b = scannerobj.nextInt();
    ask = b*c;
    System.out.println(ask);
    return ask;
}

public static void main(String [] args){
    int ask=0;

    Log(ask);

    System.out.println(ask);

}

1 Answers1

1

Because you never reset the ask variable, but rather you ignore the int that is returned by the Log method:

Log(ask); // the int returned is not assigned to anything

Instead do:

ask = Log(ask); // assign the int returned from the method back into ask.

Also understand that the ask parameter inside of the Log method is totally disconnected from the ask variable in your main method. Changing one will have no effect on the other, especially since ask is a primitive, and Java is pass by value.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373