-1

I was trying to show that I know the basics of Python with this piece of code, and it works:

x = 2
y = 4
foo = x+1
bar = y-1
S = 'SUCCESS'
F = 'FAILURE'
  
def myFunction() :
  if (foo == bar):
    return(True)
  else:
    return(False)

if myFunction():
  print(S)
else:
  print(F)

I also wanted to show my knowledge of the basics of Java with this piece of code, but it doesn't work:

public class Main {
  public static void main(String[] args) {
    int x = 2;
    int y = 4;
    int foo = x + 1;
    int bar = y - 1;
    String S = "SUCCESS";
    String F = "FAILURE";
    myMethod();
  }

  static void myMethod() {
    if (foo == bar) {
      System.out.println(S);
    } else {
      System.out.println(F);
    }
  }
}

I'm confused, because this one does work:

public class Main {
  public static void main(String[] args) {
    int x = 2;
    int y = 4;
    int foo = x + 1;
    int bar = y - 1;
    String S = "SUCCESS";
    String F = "FAILURE";

    if (foo == bar) {
      System.out.println(S);
    } else {
      System.out.println(F);
    }
  }
}

Do you guys know what's wrong?

khelwood
  • 55,782
  • 14
  • 81
  • 108
  • 6
    You declared `foo` and `bar` in `main()`, so they only exist there. If you want to use them in other methods in the class, make them class variables. – azurefrog Jun 03 '21 at 22:01
  • In python code try using foo and bar in a separate method like `def init() :` and then call `init()` followed by `myFunction()` – SomeDude Jun 03 '21 at 22:04

2 Answers2

1

In this piece of code:

public class Main {
  public static void main(String[] args) {
    int x = 2;
    int y = 4;
    int foo = x + 1;
    int bar = y - 1;
    String S = "SUCCESS";
    String F = "FAILURE";
    myMethod();
  }

  static void myMethod() {
    if (foo == bar) {
      System.out.println(S);
    } else {
      System.out.println(F);
    }
  }
}

you create the ints foo and bar, and the strings S and F inside the main function. There is no way for the myMethod() function to access foo, bar, S or F. You could add them as arguments to the myMethod() function like so:

public class Main{

  public static void main(String[] args) {
    int x = 2;
    int y = 4;
    int foo = x + 1;
    int bar = y - 1;
    String S = "SUCCESS";
    String F = "FAILURE";
    myMethod(foo, bar, S, F);
  }

  static void myMethod(int foo, int bar, String S, String F) {
    if (foo == bar) {
      System.out.println(S);
    } else {
      System.out.println(F);
    }
  }
}
  • Otherwise, you could turn `foo`, `bar`, `S` and `F` into class variables directly in the main class rather than inside the main method. – sorifiend Jun 03 '21 at 22:11
0
 static void myMethod(int foo, int bar,string S, string F) {
    if (foo == bar) {
      System.out.println(S);
    } else {
      System.out.println(F);
    }
  }

You did not pass the values into your method, so the program had to reference to what foo, bar,S or F where while running logic inside of the method. You can pass those values to method by inputting them as function parameters , as per above :) .

You would then call it like so:

myMethod(1,2,"Success","Failure");

Blockquote

StarshipladDev
  • 1,166
  • 4
  • 17