Math.random в массив, а затем распечатать массив в 10 строк по 10
Поэтому я просто пытаюсь найти способ помещения случайно сгенерированных чисел в список массивов. Я также пытаюсь просто поместить 10 чисел в строку для 10 строк, чтобы в общей сложности было напечатано 100 чисел.
Вот что я попробовал:
import java.util.*;
public class Part1 {
public static void main(String[]args){
int[] list = new int[100];
for(int j=0; j<9; j++){
System.out.println("");
for(int g=0; g<9; g++){
for(int i=0; i<list.length; i++){
int rand = (int )(Math.random() * 500 + 1);
System.out.print(rand + " ");
}
}
}
}
}
Это самый близкий, я думаю, я пришел к тому, чтобы получить что-то, что правильно
но вот еще один, который я пробовал:
import java.util.*;
public class Part1 {
public static void main(String[]args){
int[] list = new int[100];
for(int i=0; i<list.length; i++){
int rand = (int )(Math.random() * 500 + 1);
for(int j=0; j<9; j++){
System.out.println(rand + " ");
for(int g=0; g<9; g++){
System.out.print("");
}
}
}
}
}
Просто пытаюсь понять, как поместить эти случайные числа в массив, а затем можно распечатать числа в строках по 10. Спасибо за подсказку и / или помощь в продвинутом.
3 ответа
Решение
Используя одномерный массив:
Random rand = new Random();
int[] list = new int[100];
int count = 1;
for (int i = 0; i < list.length; i++)
{
list[i] = rand.nextInt(500) + 1;
// check if you're at the 10th number in the line
if (count % 10 > 0) System.out.print(list[i] + " "); // print on this line
else System.out.println(list[i] + " "); // print on a new line
count++;
}
Использование двумерного массива (рекомендуется):
Random rand = new Random();
int[][] list = new int[10][10];
for (int r = 0; r < list.length; r++)
{
for (int c = 0; c < list[r].length; c++)
{
list[r][c] = rand.nextInt(500) + 1;
System.out.print(list[r][c] + " "); // print on this line
}
// this occurs when you're done displaying each row, so skip a line.
System.out.println();
}
Использование ArrayList:
Random rand = new Random();
List<Integer> list = new ArrayList<Integer>();
int count = 1;
for (int i = 0; i < 100; i++)
{
list.add(rand.nextInt(500) + 1);
// check if you're at the 10th number in the line
if (count % 10 > 0) System.out.print(list.get(i) + " "); // print on this line
else System.out.println(list.get(i) + " "); // print on a new line
count++;
}
Наслаждаться:)
import java.util.Random;
public class Uno {
public static void main(String ... args){
Random rand = new Random();
int[] a = new int[100];
for (int i=0; i<100; i++) {
a[i] = rand.nextInt();
System.out.print(a[i] + ", ");
// perform modulo division by 10 to check if 10 results have been printed and add a new line
if( (i+1)%10==0 ) { System.out.println(); }
}
}
}
public static void main(String[]args){
int[] list = new int[100];
for(int i=0; i<list.length; i++){
int rand = (int )(Math.random() * 500 + 1);
list[i] = rand;
}
int lineNumberCounter = 0;
for(int i=0; i<list.length; i++){
if(lineNumberCounter == 10){
System.out.println();
lineNumberCounter=0;
}
System.out.print(list[i] + " ");
lineNumberCounter++;
}
}
Вы можете установить и распечатать один и тот же цикл for, изменив вышеприведенное значение на:
public static void main(String[]args){
int[] list = new int[100];
int lineNumberCounter = 0;
for(int i=0; i<list.length; i++){
int rand = (int )(Math.random() * 500 + 1);
list[i] = rand;
if(lineNumberCounter == 10){
System.out.println();
lineNumberCounter=0;
}
System.out.print(list[i] + " ");
lineNumberCounter++;
}
}