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; }}
14 Respostas
+ 5
in main method you have to add int that is equal to num and then print it. Try this.
+ 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.
+ 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);
}
}
+ 1
i dont know what is the problem with your code.. but i made that changes..
+ 1
تو متد main متغییر num رو تعریف نکردی
اینجوری بنویس
int num = addOneTo(x);
+ 1
thank u tommy
+ 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 ;)
0
thank you so much but i want know my code problem.
0
domantas i tried that,not work correctly.
0
اره درسته اینکارو بکنم کار میکنه ولی الان چرا اجرا نمیشه num تعریف شده تو اون تابع بعدشم return شده. میخوام ببینم این چرا کار نمی کنه.
ایرانیا همه جا هستنا 😉
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;
}
}
0
I don't find any problem in your code. it runs successfully.
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
0
define "addOneTo(x);" is it a method or what?