Java -- Iterations not working -



Java -- Iterations not working -

i doing java game that's cows , bulls. instead of displaying "cow" , "bull" displays "moo" or "moo." 4 digit number generated , user can guess it. each digit correctly specified (same number, same position), "moo" displayed on screen. each digit guessed has same number, wrong position "moo" displayed. i'm having problem doing method returns how many "moo's" returned.

public int getlittlemoocount(int guess) { int count = 0; string guessstring = integer.tostring(guess); string randomvaluestring = integer.tostring(randomvalue); // pads number 0 if less 4 digits, length 4 while(guessstring.length() < 4) { guessstring = "0" + guessstring; } while(randomvaluestring.length() < 4) { randomvaluestring = "0" + randomvaluestring; } // checking see if positions match. if so, homecoming moo (the getbigmoo // count in other method) (int = 0; < 4; i++) { if (guessstring.charat(i) == randomvaluestring.charat(i)) { randomvaluestring = randomvaluestring.replace(randomvaluestring.charat(i), 'x'); guessstring = guessstring.replace(guessstring.charat(i), 'o'); } } (int = 0; < 4; i++) { (int j = 0; j < 4; j++) { if (guessstring.charat(j) == randomvaluestring.charat(i)) { count++; randomvaluestring = randomvaluestring.replace(randomvaluestring.charat(i), 'x'); guessstring = guessstring.replace(guessstring.charat(j), 'o'); } } } homecoming count; } inputs testing 0011 2233 4455 6677 8899 1234 5678 9090

and results w/ random number of 5509

1 moo = correct nothing = correct 1 moo = wrong! should 2 moo's. nothing = correct 1 moo = correct nothing = correct 1 moo = correct 2 moo = correct

it seems work numbers, others, there 1 test info wrong. obviously, wrong, tried , it's frustrating! can post illustration if helpful.

thanks help in advance!

edit: alright, changed character array, , doesn't anything. of swore did right.

int count = 0; string guessstring = integer.tostring(guess); string randomvaluestring = integer.tostring(randomvalue); char [] randomvaluecharacter = randomvaluestring.tochararray(); char [] guesscharacter = guessstring.tochararray(); while(guessstring.length() < 4) { guessstring = "0" + guessstring; } while(randomvaluestring.length() < 4) { randomvaluestring = "0" + randomvaluestring; } (int = 0; < 4; i++) { if (guessstring.charat(i) == randomvaluestring.charat(i)) { randomvaluecharacter[i] = 'x'; guesscharacter[i] = 'o'; } } (int = 0; < 4; i++) { (int j = 0; j < 4; j++) { if (guessstring.charat(j) == randomvaluestring.charat(i)) { count++; randomvaluecharacter[i] = 'x'; guesscharacter[j] = 'o'; } } } homecoming count; }

it because of string.replace(). function not replace character @ specific position assume, replaces apperances of given character! after first 5 found, both 5's replaced x.

javadoc

public string replace(char oldchar, char newchar) returns new string resulting replacing occurrences of oldchar in string newchar. if character oldchar not occur in character sequence represented string object, reference string object returned. otherwise, new string object created represents character sequence identical character sequence represented string object, except every occurrence of oldchar replaced occurrence of newchar.

examples:

"mesquite in cellar".replace('e', 'o') returns "mosquito in collar" "the war of baronets".replace('r', 'y') returns "the way of bayonets" "sparring violet porpoise".replace('p', 't') returns "starring turtle tortoise" "jonl".replace('q', 'x') returns "jonl" (no change) parameters: oldchar - old character. newchar - new character. returns: string derived string replacing every occurrence of oldchar newchar.

http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/string.html

you utilize integer array instead.

try this

int count = 0; string guessstring = integer.tostring(4455); string randomvaluestring = integer.tostring(5509); char [] randomvaluecharacter = randomvaluestring.tochararray(); char [] guesscharacter = guessstring.tochararray(); while(guessstring.length() < 4) { guessstring = "0" + guessstring; } while(randomvaluestring.length() < 4) { randomvaluestring = "0" + randomvaluestring; } (int = 0; < 4; i++) { if (guesscharacter[i] == randomvaluecharacter[i]) { randomvaluecharacter[i] = 'x'; guesscharacter[i] = 'o'; } } (int = 0; < 4; i++) { (int j = 0; j < 4; j++) { if (guesscharacter[j] == randomvaluecharacter[i]) { count++; randomvaluecharacter[i] = 'x'; guesscharacter[j] = 'o'; } } } homecoming count; }

java iteration

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -