+ 1
Someone explain this in simple way ..y the answer is 5 not 6?
public class MyClass { public static void main(String[ ] args) { int x = 5; addOneTo(x); System.out.println(x); } static void addOneTo(int num) { num = num + 1; } }
10 Respostas
+ 2
wait what was that john???
u r using * and & in java??
java has no such conventions
-_- -_-
+ 2
it will work in c++ right ?
+ 1
public class MyClass {
public static void main(String[ ] args) {
int x = 5;
int c;
c=addOneTo(x);
System.out.println(c);
}
static int addOneTo(int num) {
num = num + 1;
return num;
}
}
addoneTo function needs to return the incrememted value...
else num the change of x wont occur in main func...
if u want to do in either
u need to tae x as global var..
+ 1
Pointers are not supported in Java, so @John's Idea won't work.
+ 1
ya cpp has pointer...
that ans has been marked as best ans...đšđšđ”đ”OMG
0
Anyways, you could also try this:
public class MyClass {
static int x;
public static void main(String[ ] args) {
x = 5;
addToX ();
System.out.println(x);
}
static void addToX (){
x++;
}
}
The function does not work with other variables as x here, so @sayans idea might be better for your uses.
0
I posted the same code as yours @sayan
0
Ok, your pointđ
- 1
this is another way...
public class MyClass
{
static int x;
public static void main(String[ ] args)
{
x = 5;
addToX ();
System.out.println(x);
}
static void addToX ()
{
x++;
}
}
- 1
no you r not...
1--the code needs a static before int...
ur code wont work...
2--also u have printed c
which is not in code..