public class Solution {
public static void main(String[] args) {
//write your code here
convertEurToUsd(12,23.2);
convertEurToUsd(3,4.7);
System.out.println(dollar);
return dollar;
}
public static double convertEurToUsd(int eur, double exchangeRate) {
//write your code here
double dollar = eur * exchangeRate;
Dona
Level 7
can anyone help me with this code?
Under discussion
Comments (6)
- Popular
- New
- Old
You must be signed in to leave a comment
Isaiah Burkes
22 May 2019, 01:52
Put the return statement within your convertEurToUsd method.
Put your method with the parameters ex. method(int, double) inside of the print statement that is in the main method.
0
Thomas
20 May 2019, 20:58
This should help --suggest read instruction very carefully
* no print allowed in currency exchange logic block
* .... then option is launch print ( ) from main ( ) method
* stuff in the argument values their when called in print ( arg arg arg )
https://codegym.cc/tasks/com.codegym.task.task03.task0303
0
Andrew
20 May 2019, 19:23
A lot of students new to programming get confused about printing versus returning. As I look at your code, you have the line "public static void main(String[] args) {" ...which has the word void in it. Here, I uppercased it to make it stand out:
"public static VOID main(String[] args) {"
That means you cannot have a statement within that method that tries to return a value. So, "return dollar;" is an error. The code won't compile. You would be allowed to say "return;" meaning the method would end when it reaches that line, and control would return to the operating system, without a value being returned.
Next, there is an issue called "variable scope," or "the scope of a variable." You've declared "dollar" in the convertEurToUsd method, but that variable isn't visible outside of that method. Your main method has the line "System.out.println(dollar);" and "return dollar;" and yet main has no idea what you're talking about. It doesn't know what "dollar" is. Doesn't even know it's a variable.
You need to make the convertEurToUsd method return a value and then you need to capture that in a variable inside main.
// This needs to be inside main() ...
double dollar = convertEurToUsd( 3, 4.7 );
System.out.println(dollar);
main() and convertEurToUsd() can both have a variable called "dollar" and they won't know that each other has a variable of the same name. They won't overwrite each other accidentally, they're stored in separate areas of memory. Whether you use the same name for both variables or pick unique names for them is just a choice of preference, not affecting how data flows.
+2
Thomas
20 May 2019, 20:18
Good post on VOID & Variable Scope ( access ) Required Reading for JAVA using main ( ) & writing methods ... which is quirky coming from Javacript ! Thanks
+1
Gab Camba
20 May 2019, 09:09
/* The comment was deleted */
+1
Dona
21 May 2019, 01:56
i have error in 3rd condition:The main method should display the result of the calls, each time on a new line.
0