+ 4
Can someone explain this code, please? TIA
int A = 5; int N = 3; int D; D = calcValue(A); System.out.println(D); static int calcValue (int N) { int D; for ( D = 1; D <5; D++) { N++; } return (N); } 9
16 Respostas
+ 8
Ok, fairly simple:
"D = calcValue(A);" the value of "D" will be that the "calcValue(A)" returns considering that the parameter "A" with value of "5", which means "calcValue(5)" is the same thing.
static int calcValue (int N) {//The parameter provide is 5
int D;// " D is declared inside the method's body, hiding the "D" variable declared outside of it.
for ( D = 1; D <5; D++) {//The first value of D is 1, then the condition D < 5 is checked true
N++;// "N which is originally 5 becomes 6 and so on until the condition is false
//After the body then "D++" changing D's value by 1 per iteration until condition is false
}//The loop goes on 4 times because when D becomes 5 then the condition is false
return (N);// 5 was the original parameter value, plus 4, this method returns 9
}
Hope this explanation helps you. Good luck with your studies.
+ 11
public class Program
{
public static void main(String[] args) {
int A = 5;
int N = 3; //this line not required in code, extra line
int D;
D = calcValue(A); //calcValue(5)
System.out.println(D);
}
static int calcValue (int N){
//N=5 here
int D;
for ( D = 1; D <5; D++){
N++;
//loop runs 4 times, so N=N+1 , four times.
}
//N=5+4=9 after loop ends.
return (N);
}
}
+ 11
Denise RoĆberg
There might be white space or extra spaces, that is why sometimes error comes [I see this case, with many questions having same problem]
//for seeing those spaces, just copy link to code & paste in google & then see the code, those will be visible there & u can remove them & code will start working good.
+ 8
Atila Sabo I am no expert, get to know about that from friends here only : )
+ 3
Denise RoĆberg Yes! Your code works, but the code I posted doesn't. I really don't undestand...there are some tricks in SL Code Playground. Maybe somebody else knows Danijel IvanoviÄ Gaurav Agrawal LukArToDo
+ 3
Atila Sabo I think something went wrong when copying and pasting the code. After I deleted everything from line 25 and rewritten it by hand, there were no more problems with the comments.
+ 3
Gaurav Agrawal thank you! I knew you are expert ;)
+ 2
Atila Sabo It's interesting. The code works also on SL but I can not find any errors in your code.
Edit:
public class Program
{
public static int calcValue(int N){
int D;
for(D = 1; D < 5; D++){
N++;
}
return N;
}
public static void main(String[] args) {
int A = 5;
int N = 3;
int D;
D = calcValue(A);
System.out.println(D);
}
}
+ 2
Atila Sabo I'm not sure if it does matter where the method stands.
calcValue (){
}
public static void main... (){
...
}
Or:
public static void main... (){
calcValue(){
....
}
}
+ 2
Denise RoĆberg As far as I know it doesn't metter. I tried it anyway, but it still doesn't work
+ 2
Atila Sabo I got it: remove the comments in line 28 and 31
edit: but I can not explain why ;)
edit2: maybe not the comments itself are the problem but the copy&paste
+ 2
Denise RoĆberg great work bro! That's it!!! I'm not sure, but maybe SL Code Playgroung doesn't accept comments outside the class (when I put it inside class it works)
+ 2
Gaurav Agrawal Thanks :)
+ 2
Code fixed :) thank you all for help
+ 1
Roberto Guisarre
I tried to run this code with SL Code Playground but it shows me An error occurred! Do you know why? BTW very nice and clear explanation.
P.s. I think that it's not necessary to make int N (line 6) because it does nothing right?
https://code.sololearn.com/c9A4gHTNNHCG/?ref=app
EDIT: Never mind, it works in eclipse
+ 1
Gaurav Agrawal That's great, now we can learn more form you ;) thanks and all the best