Java Random Arrays -


im working on class, , i'm stuck. believe im pretty close, dont know go. when run code im getting arrays ten times, must have loop. also, in arrays, im not sure how create range 20-50. mine 1-50. lastly, output not needed. appreciated. here question, followed code far.

create array named array1 10 random integer numbers in range of [20 50]. create array named array2 same size of array1. copy numbers in array1 greater 35 array2. pad 0s in array2 if not enough numbers copied it. example, if array1 {34, 23 45, 39, 28, 41, 33, 23, 42, 48}, array2 be{45, 39, 41, 42, 48, 0,0,0,0,0}

import java.util.random; import java.util.scanner; import java.util.arrays;  public class arraylab6 {     public static void main(string[] args) {         int x;         int[] array1 = new int[10];         int[] array2 = new int[10];         random rand = new random();         (int = 0; < array1.length; i++) {             int h = rand.nextint(50);             array1[i] = h;         }         system.out.println(arrays.tostring(array1));         (int = 0; < array1.length; i++) {             if (array1[i] > 35) {                 array2[i] = array1[i];             } else {                 array2[i] = 0;             }             system.out.println(arrays.tostring(array1));             system.out.println(arrays.tostring(array2));         }     } } 

im not sure how create range 20-50. mine 1-50.

int h = rand.nextint(31)+20; //nextint(31) = range 0-30, add 20 result 

when run code im getting arrays ten times

your println calls in for loop, move them down.

also, solution not satisfy requirements, because you're putting number >35 same index other array. instead, keep track of insert index, , move when copy int:

int copyindex = 0; (int = 0; i< array1.length; i++) {   if (array1[i]>35){     array2[copyindex++]=array1[i]; //add 1 copyindex each time value copied } 

you don't need "pad" zeroes because int's default value 0.


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -