What's the problem? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What's the problem?

public class MyClass { public static void main(String[ ] args) { int x = 5; addOneTo(x); System.out.println(num); } static int addOneTo(int num) { num = num + 1; return num; }}

29th Jul 2016, 8:00 AM
mehdi
14 Answers
+ 5
in main method you have to add int that is equal to num and then print it. Try this.
29th Jul 2016, 8:46 AM
Domantas
+ 4
public class MyClass { public static void main(String [] args){ int x = 5; int num = addOne(x); System.out.println(num); } static int addOne(int num){ num = num + 1; // num++; return num; } } Chech line 4: The method addOne() returns an int value. That returned value needs to go into a variable. The num-variable inside the addOne()-method has a scope that is only within that method. The num-variable in the main-merhod has a scope that is within that meghod. The two num-variables are unique even though they are called the same name.
29th Jul 2016, 9:25 AM
Tommy Taraldsvik
Tommy Taraldsvik - avatar
+ 1
public class MyClass { public static void main(String[ ] args) { int x = 5; addOneTo(x); // System.out.println(num); } static void addOneTo(int num) { num = num + 1; // return num; System.out.println(num); } }
29th Jul 2016, 8:26 AM
Emmanuel
Emmanuel - avatar
+ 1
i dont know what is the problem with your code.. but i made that changes..
29th Jul 2016, 8:28 AM
Emmanuel
Emmanuel - avatar
+ 1
تو متد main متغییر num رو تعریف نکردی اینجوری بنویس int num = addOneTo(x);
29th Jul 2016, 9:00 AM
mehdi.sp
mehdi.sp - avatar
+ 1
thank u tommy
29th Jul 2016, 9:41 AM
mehdi
+ 1
or this is also possible: public class MyClass { public static void main(String[] args){ int x = 5; x = addOneTo(x); System.out.println(x); } static int addOneTo(int num){ num = num + 1; return num; } } if you read my code, than it should be clear, where the problem was ;)
6th Aug 2016, 11:00 PM
Jessica
Jessica - avatar
0
thank you so much but i want know my code problem.
29th Jul 2016, 8:47 AM
mehdi
0
domantas i tried that,not work correctly.
29th Jul 2016, 8:52 AM
mehdi
0
اره درسته اینکارو بکنم کار میکنه ولی الان چرا اجرا نمیشه num تعریف شده تو اون تابع بعدشم return شده. میخوام ببینم این چرا کار نمی کنه. ایرانیا همه جا هستنا 😉
29th Jul 2016, 9:04 AM
mehdi
0
I would change your code this way: public class MyClass { public static void main(String[] args){ int x = 5; System.out.println(addOneTo(x)); } static int addOneTo(int num){ num = num + 1; return num; } }
6th Aug 2016, 10:46 PM
Jessica
Jessica - avatar
0
I don't find any problem in your code. it runs successfully.
15th Aug 2016, 10:24 AM
Gowtham krvz kv
Gowtham krvz kv - avatar
0
I think problem is You didn't print x, the method Add() just return the value. That mean x=6, but you still didn't print x. You should use System....("x") after call the method. Btw, sorry about my English. :P
19th Aug 2016, 3:43 PM
Lê Thị Linh
Lê Thị Linh - avatar
0
define "addOneTo(x);" is it a method or what?
15th Jun 2017, 6:51 AM
varun sharma
varun sharma - avatar