arrays - I need assistance with my intro to Java assignment -
i have compiled java program (which generates 100 random numbers between 0 , 25, puts them in array, , sorts them 2 different arrays based on whether each or odd), although not run. suspect have made mistake 1 of while loops, although don't know sure. also, struggled code in formatted in question, tabs off, still legible. here .java text:
public class assignment8 { public static void main( string [] args ) { int storage [] = new int[100]; int j = 0; while ( storage.length < 100 ) { int testvariable = 0 + (int) (math.random() * ((25 - 0) + 1)); storage[j] = testvariable; j++; } int oddarray[] = oddnumbers( storage ); int evenarray[] = evennumbers( storage ); int currentnumber = 0; system.out.println( "the odd numbers are: " + "\n" ); while ( currentnumber <= 99 ) { system.out.println( oddarray[currentnumber] + "\n" ); currentnumber++; } system.out.println( "\n" + "the numbers are: " + "\n" ); currentnumber = 0; while ( currentnumber <= 99 ) { system.out.println( evenarray[currentnumber] + "\n" ); currentnumber++; } } public static int[] oddnumbers( int storage[] ) { int currentnumber = 0; int currentvalue = storage[currentnumber]; int oddarray[] = new int[100]; while ( currentnumber <= 99 ) { if ( storage[currentnumber] % 2 != 0 ) { oddarray[currentnumber] = currentvalue; } else { continue; } currentnumber++; } return oddarray; } public static int[] evennumbers( int storage[] ) { int currentnumber = 0; int currentvalue = storage[currentnumber]; int evenarray[] = new int[100]; while ( currentnumber <= 99 ) { if ( storage[currentnumber] % 2 == 0 ) { evenarray[currentnumber] = currentvalue; } else { continue; } currentnumber++; } return evenarray; } }
storage.length
not change throughout program's execution, array allocated. first while
loop wrong, 100 not less 100, never execute. instead, use simple for
loop:
for (int j = 0; j < storage.length; ++j) { int testvariable = 0 + (int) (math.random() * ((25 - 0) + 1)); storage[j] = testvariable; }
Comments
Post a Comment