Question

package array; public class Test { static int[] data = (0,1,2,3,4,5,6,7,8); public static void main (String[]...

package array;

public class Test {

static int[] data = (0,1,2,3,4,5,6,7,8);

public static void main (String[] a) {

for ( int i = 0;i<data.length; i++) {

if(i %3 == 0) {

System.out.print("A");

System.out.print(data[i]);

System.out.print(" ");

}

}

I need an explanation of what this code is doing ? is there anything wrong with it

}}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// correct after removing syntax error

public class Test {

static int[] data = {0,1,2,3,4,5,6,7,8};

public static void main (String[] args)

{

for ( int i = 0;i<data.length; i++) {

if(i %3 == 0) {

System.out.print("A");

System.out.print(data[i]);

System.out.print(" ");

}

}

}}

output:- A0 A3 A6

EXPLANATION:- SINCE ARRAY IS STATIC SO IT CAN ASSCESS ANY METHOD SO FOR WILL GET EXECUTE AND WHEN I%3 ==0 FOR I =0,3,6 IT WILL PRINT A AND THEN DATA AT data[0] which will print A0 similarly for i =3 and 6

Add a comment
Know the answer?
Add Answer to:
package array; public class Test { static int[] data = (0,1,2,3,4,5,6,7,8); public static void main (String[]...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT