+ 1

Could someone helps me?😂 I got some trouble in java array.Please!🙏

public class Java{ public static void main(String[] args) { int[] a=new int[5]; for(int i=0; i<a.length;i++) a[i]=i+1; System.out.println("a[" +i+"]" + "="+a[i]); } }

17th Jun 2018, 7:17 AM
Hunt
Hunt - avatar
7 odpowiedzi
+ 5
Hi, sorry for the delay, I was knocked out for hours, well, I have re-test the code, I am guessing you might have invalid character copied along with the code probably, that might be the cause for compilation error. Can you run this again just to check: public class Program { public static void main(String[] args) { int[] a=new int[5]; for(int i=0; i<a.length;i++){ a[i]=i+1; System.out.println("a[" + i + "]=" + a[i]); } } } You can find me in SoloLearn Discord server, there are more Java knowledgeable people there too, see this thread for more info about Discord: https://www.sololearn.com/Discuss/689391/?ref=app Server invite link: https://discord.gg/MEjFNcC
18th Jun 2018, 2:21 PM
Ipang
+ 7
You forgot to add curly braces to wrap the for loop body, remember when there are more than one instructions line in a block you need to wrap them in curly braces : ) public class Java{ public static void main(String[] args) { int[] a=new int[5]; for(int i=0; i<a.length;i++){ a[i]=i+1; System.out.println("a[" +i+"]" + "="+a[i]); } } } Hth, cmiiw
17th Jun 2018, 8:32 AM
Ipang
+ 4
Oh, sorry about that Hunt , can you tell me if you get any error message running the code? I tried to run that code in Code Playground and got result as follows: a[0]=1 a[1]=2 a[2]=3 a[3]=4 a[4]=5 Maybe we'll work something out, come and talk to me : )
18th Jun 2018, 3:47 AM
Ipang
+ 1
Hi,nice to meet you. But it still did not work😂
17th Jun 2018, 2:11 PM
Hunt
Hunt - avatar
+ 1
Yeah i follow your way to do it,but the output of my program always show "Compilation error". This is my program👇👇👇👇 public class Program { public static void main(String[] args)  {        int[] a=new int[5];           for(int i=0; i<a.length;i++){      a[i]=i+1;      System.out.println("a[" +i+"]" + "="+a[i]);      } } } By the way,how can i talk to you personnally😂
18th Jun 2018, 6:23 AM
Hunt
Hunt - avatar
+ 1
Thank you,your program works!
18th Jun 2018, 3:27 PM
Hunt
Hunt - avatar
+ 1
Ok,i will find you guy!
18th Jun 2018, 3:30 PM
Hunt
Hunt - avatar