|
-
Apr 7th, 2009, 08:11 PM
#1
Thread Starter
Member
Help scrambling a word
I'm fairly new to Java and I need help scrambling a word. I made the following but for some reason the if statement inside the do loop isn't working.
Code:
import java.util.Random;
class scramble {
public static void main(String[] args) {
String word,newword;
int rndnum;
Random randGen = new Random();
word = "tiger";
newword="";
boolean letter[] = new boolean[word.length()];
do {
rndnum = randGen.nextInt(word.length());
if (letter[rndnum] = false) {
newword = newword + word.charAt(rndnum);
letter[rndnum] = true;
}
} while (newword.length() < word.length());
System.out.println(newword);
}
}
Anyone know why? I printed the status of letter[rndnum] and it's always returning false, so I'm not sure why the if isn't working.
-
Apr 8th, 2009, 01:15 AM
#2
Re: Help scrambling a word
"letter[rndnum] = false" This line sets the array element to false and of course it's a false condition, so the code within the if is never executed
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Apr 8th, 2009, 05:55 AM
#3
Thread Starter
Member
Re: Help scrambling a word
Oh I see, it should be ==...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|