+ 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

11th Jan 2019, 8:58 PM
Jessica
16 odpowiedzi
+ 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.
11th Jan 2019, 9:52 PM
Roberto Guisarre
Roberto Guisarre - avatar
+ 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); } }
13th Jan 2019, 11:30 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 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.
13th Jan 2019, 11:38 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 8
Atila Sabo I am no expert, get to know about that from friends here only : )
13th Jan 2019, 12:54 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 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
13th Jan 2019, 10:25 AM
Atila Sabo
Atila Sabo - avatar
+ 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.
13th Jan 2019, 11:37 AM
Denise Roßberg
Denise Roßberg - avatar
+ 3
Gaurav Agrawal thank you! I knew you are expert ;)
13th Jan 2019, 12:00 PM
Atila Sabo
Atila Sabo - avatar
+ 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); } }
13th Jan 2019, 9:53 AM
Denise Roßberg
Denise Roßberg - avatar
+ 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(){ .... } }
13th Jan 2019, 10:45 AM
Denise Roßberg
Denise Roßberg - avatar
+ 2
Denise Roßberg As far as I know it doesn't metter. I tried it anyway, but it still doesn't work
13th Jan 2019, 10:56 AM
Atila Sabo
Atila Sabo - avatar
+ 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
13th Jan 2019, 11:03 AM
Denise Roßberg
Denise Roßberg - avatar
+ 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)
13th Jan 2019, 11:22 AM
Atila Sabo
Atila Sabo - avatar
+ 2
Gaurav Agrawal Thanks :)
13th Jan 2019, 11:45 AM
Denise Roßberg
Denise Roßberg - avatar
+ 2
Code fixed :) thank you all for help
13th Jan 2019, 12:47 PM
Atila Sabo
Atila Sabo - avatar
+ 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
12th Jan 2019, 4:51 PM
Atila Sabo
Atila Sabo - avatar
+ 1
Gaurav Agrawal That's great, now we can learn more form you ;) thanks and all the best
13th Jan 2019, 12:56 PM
Atila Sabo
Atila Sabo - avatar