Tuesday 15 March 2011

Check if all values in an array are not equal (java) -



Check if all values in an array are not equal (java) -

for code check array filled many times 6 random integers. each time want check if 6 values different , if happens code stop. after trying utilize loops having problems used long ass if statement check each location in array each other location. not working. love if tell me why i'm beingness stupid.

if((dicearray[0] != dicearray[1] & dicearray[0]!=dicearray[2] & dicearray[0]!=dicearray[3] & dicearray[0]!=dicearray[4] & dicearray[0]!=dicearray[5] & dicearray[1]!=dicearray[2] & dicearray[1]!=dicearray[3] & dicearray[1]!=dicearray[4] & dicearray[1]!=dicearray[5] & dicearray[2]!=dicearray[3] & dicearray[2]!=dicearray[4] & dicearray[2]!=dicearray[5] & dicearray[3]!=dicearray[4] & dicearray[3]!=dicearray[5] & dicearray[4]!=dicearray[5])) { system.out.println("stop babes"); } else { system.out.print("did not work"); }

there more code know rest works when print out each sequence pint out no problem. when run code part ideally print out many sequences duplicate number , these print out "did not work" , 1 of results should come out "stop babes" (these removed later using them test , homecoming used stop code) when run code happens

how many rolls of 6 dice? 3 line 1 4 1 3 5 5 5 stop babes line 2 4 4 1 2 6 1 stop babes line 3 3 6 4 6 4 4 stop babes

just quick edit re on if statement. realise shortened quick loop wanted brute forcefulness problem. main problem text "stop babes" should printing when code such 1 2 3 4 5 6 printed informing me code works , allowing me insert break statement on main loop.

if ultimate goal remove sequences have duplicates, should consider checking array @ time fill it:

instead of doing (you didn't provide code i'm guessing):

n = rand.nextint(); dicearray[nextposition++] = n;

do like:

n = rand.nextint(); ( int = 0; < nextposition; i++ ) { if ( dicearray[i] == n ) { return; // or whatever cause array disqualified. } }

this way save rolling numbers won't utilize anyway. suppose rolled 2 2. know have disqualify array, don't need go on rolling 2 2 1 3 4 5 , loop through whole thing , disqualify in end.

this true if want count number of bad arrays got before got array.

random rand = new random(); int[] dicearray = new int[6]; int badarrays = 0; int nextposition = 0; while ( nextposition < 6 ) { boolean goodmove = true; int n = rand.nextint(6) + 1; ( int = 0; < nextposition; i++ ) { if ( dicearray[i] == n ) { goodmove = false; break; } } if ( goodmove ) { dicearray[nextposition++] = n; } else { nextposition = 0; badarrays++; } } system.out.println( "got array: " + arrays.tostring(dicearray) + " after " + badarrays + " failures." );

java arrays

No comments:

Post a Comment