Neal Gamby Neal Gamby - 8 months ago 130
Java Question

Can I transfer an int from my main method to another method?

//I need help transferring int x and int y to DistanceReturner (I'm new to Java)

public class CGT {
public static int main(String[] args) {
Scanner scan = new Scanner(System.in);
int x = scan.nextInt();
int y = scan.nextInt();
int[] Array;
Array = new int[2];
Array[0] = x;
Array[1] = y;

int x2 = scan.nextInt();
int y2 = scan.nextInt();
int[] AArray;
AArray = new int[2];
AArray[0] = x2;
AArray[1] = y2;

}// main method
public static int DistanceReturner;
{

int dis = Math.abs(x - y);

}// DistanceReturner

}// public class

Answer Source

First off, it's public static void main(String[] args), 2nd I fixed all of your errors in your distanceReturner method!

public class CGT {

// main method
public static int main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int x = scan.nextInt();
    int y = scan.nextInt();

    distanceReturner(x, y);
}

 // distanceReturner method
 public static int distanceReturner(int x, int y)
 {

    int dis = Math.abs(x - y);

    return dis;
 }

}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download