Write a Java program that will create an array of 100 integers. Fill the array w
ID: 3684213 • Letter: W
Question
Write a Java program that will create an array of 100 integers. Fill the array with randomnumbers between 1 and 1000. After the array has been filled, print the array in nice, neat, right-aligned columns, 10 numbers per line.
So this is the directions I am trying to follow for this code, however my code doesnt seem to even get the array part correct and I cant seem to get it to print everything i need, let alone the 10 numbers per line. If anyone could look over my code and show me how to fix it, I would greatly apprectiate it. Thank you.
public class RandomColumns {
public static void main(String[] args) {
// declares an array of integers
int[] anArray;
// allocates memory for 10 integers
anArray = new int[10];
int [] array = new int[100]; // Sorted Array of 100
for (int a = 0; a < array.length; a++) {
array[a] = (int) (Math.random()*1+1001);
}
System.out.println("Element at index 0: "
+ anArray[a]);
}
}
Explanation / Answer
import java.util.Random;
public class RandomColumns
{
public static void main(String[] args)
{
// declares an array of integers
Random r=new Random();
int[] anArray=new int[100];
for (int a = 0; a < anArray.length; a++)
{
anArray[a] = r.nextInt(1000)+1;
}
for(int i=0;i<anArray.length;i++)
{
if(i%10==0)
System.out.println("");
System.out.print(anArray[i]+" ");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.