Hey all, I have a program that repeatedly flips a coin (Heads or Tails) until 3 consecutive Heads occur, at which point the program will display how many flips it took. I am new to Java programming and was hoping if someone could look over my program. To me, the program looks a bit messy, so if there is a different way that you would have written it more cleanly, please let me know. Thanks
1. I think it is a good idea to always use equals() to compare strings. In this case, it works because you are only using string literals -- and the string pool is helping, but this will quickly break if you do any string operation.
2. Instead of having three embedded conditionals, can you get it to work with another counter? Heads would increment it, and tails will set it back to zero. And of course, you would need a check to exit the loop when the count goes to three.
I didn't test or compile this but it should work (and it's not the best implementation)
1) Try using enumerations instead of String or Integers or whatever when you have a limited number of possibilities
2) Try avoiding infinite loops , even if you use a break statement in there (good habit)
3) If you must use an infinite loop (who knows) go with while(true) if you don't have statements after the loop or you will get an "unreachable code compile error".
4) Be afraid of nested "if's" they are evil. In big applications it can be a pain in the you know where.
5) Don't use the run() method if you are not working with threads it's confusing (good practice again) does program class extend thread?