Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using a For Loop to Assign Values to an Array in Java

I need to take an Array with 10 int and initialize them all to 0. Then I need to write a For loop that changes all their values to range 10-100 counting by tens. Then another For loop to print them all. How do I make them count by ten? and how do I print them in the second loop?

    int i
    int[] numsTen= new int [10];
    numsTen[0]= 0;
    numsTen[1]= 0;
    numsTen[2]= 0;
    numsTen[3]= 0;
    numsTen[4]= 0;
    numsTen[5]= 0;
    numsTen[6]= 0;
    numsTen[7]= 0;
    numsTen[8]= 0;
    numsTen[9]= 0;
    
    for (i=10; i<100; i+10) {
    numsTen[i]=i; 
    
    for (i = 0; i=numsTen; i+10) {
        System.out.print(numsTen[] + " ");;
    }
    }
}

}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Here is the corrected code:

int i;
int[] numsTen = new int [10];

for (i = 0; i < 10; i++) {
    numsTen[i] = i * 10 + 10;
}

for (i = 0; i < 10; i++) {
    System.out.print(numsTen[i] + " ");
}

In the first for loop, the value of i is used as the index for the numsTen array, and the value of i * 10 + 10 is assigned to that index. This effectively increments the value of numsTen[i] by 10 for each iteration of the loop, starting from 10.

In the second for loop, the value of i is again used as the index for the numsTen array, and the value of numsTen[i] is printed for each iteration of the loop.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading