Author |
Message |
Krocker
|
Posted: Wed Oct 16, 2013 3:43 pm Post subject: Methods |
|
|
im creating a program which requires me to pass 2 variables to an another method in the same class. However, i have no idea how to do it as its my 1st time working with multiple methods. So i have one method that collects 2 double variables from the user in the main method, then i need to transfer those two variables to another method that does some calculations. how would i transfer those 2 variables? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
DemonWasp
|
Posted: Wed Oct 16, 2013 7:00 pm Post subject: RE:Methods |
|
|
In what way are the first method and the second method different?
Why can't you pass the arguments from (first) to (second) the same way you pass from (main) to (first)? |
|
|
|
|
|
Krocker
|
Posted: Wed Oct 16, 2013 7:22 pm Post subject: RE:Methods |
|
|
i dont quite understand what u mean by the same way as first and second. Im required to do an assignment that asks to create 2 methods, one that does the calculation and the other gets the input. however, there are 2 input variables and i dont know how to transfer the variables from one method to another. |
|
|
|
|
|
DemonWasp
|
Posted: Wed Oct 16, 2013 7:34 pm Post subject: RE:Methods |
|
|
Here's an example for one argument, of type int. You should be able to extrapolate...
Java: |
public class Example {
public static void main ( String[] args ) {
System. out. println ( methodOne (5) );
}
private static int methodOne ( int x ) {
return x * x;
}
}
|
|
|
|
|
|
|
Krocker
|
Posted: Wed Oct 16, 2013 7:50 pm Post subject: RE:Methods |
|
|
ok, so wat if theres 2 variables such as x and y, how would i do that? |
|
|
|
|
|
Nathan4102
|
Posted: Wed Oct 16, 2013 7:56 pm Post subject: Re: Methods |
|
|
Replace the 5 in the main method with your variable. If you have multiple variables, make the method accept multiple parameters.
If you aren't sure how to do that, this site summaries it. |
|
|
|
|
|
Krocker
|
Posted: Wed Oct 16, 2013 8:00 pm Post subject: RE:Methods |
|
|
oh ok |
|
|
|
|
|
|