I'm trying to make a coin flip game in java. I'm relatively new to the language and the only other language I knew was Javascript, so I'm still learning. I've already made one before using just one class and putting all the code inside, but I'm now trying to do it with methods since I'm trying to learn them.
The basic idea of the game is that the user picks how many coins they'd like to flip, they pick heads or tails, then the computer flips the coins and calculates if there was more heads or tails to tell the user if they won or not.
I'm not quite complete with the program as you can see but I've already run into an error. I was just doing some tests on the code I already have and I found that when I call the method settingUpCoin
in my main class, the program terminates.
So basically, when I run it, it executes userImp
right, transform right, but then it doesn't let you enter a value for howManyCoins
and terminates before you get to settingUpCoin
. Please help! Thank you! (Also, sorry if I said something really stupid in that explanation I'm really new to programming in general so please humor me :D)
By the way, if you have any other suggestions or tips to help me please feel free to give them just don't reveal too much because I want to see how much I can figure out on my own. Once again, thanks for all the help because I know this is a huge messy post that you all are spending your own free time to read!
(Notes: this was all done in Eclipse Luna build or the Java IDE. The class files are all separate in the actual thing, I just put them together here to demonstrate my code.)
Coin.java:
import java.io.IOException;
public class Coin {
double myCoin;
int numOfCoins;
int counter;
double arrayOfCoins[] = {};
public void howManyCoins() throws IOException {
System.out.println("How many coins would you like to flip?");
numOfCoins = System.in.read();
}
public void settingUpCoin() {
for (counter = 0; counter == numOfCoins; counter++) {
arrayOfCoins[counter] = Math.floor(Math.random() * 2);
System.out.println("Adding a coin. Coin number" + counter);
//each time loop goes consider calling userChoice() method
}
}
}
UserChoice.java:
import java.io.IOException;
public class UserChoice {
char userPick;
int finalUserPick;
public void userImp() throws IOException {
System.out.println("Pick H for heads and T for tails. Make sure to capitalize."); // times number of coins?
userPick = (char) System.in.read(); // times number of coins?
}
public void transform() {
if (Character.toString(userPick).matches("H")) {
finalUserPick = 0;
System.out.println("You picked Heads");
} else {
finalUserPick = 1;
System.out.println("You picked Tails");
}
}
}
PutItAllTogether.java:
import java.io.IOException;
public class PutItAllTogether {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Coin myCoin1 = new Coin();
UserChoice request = new UserChoice();
request.userImp();
request.transform();
myCoin1.howManyCoins();
myCoin1.settingUpCoin();
}
}
Try using this for getting the number of coins:
Note that you will have to import java.io.BufferedReader.
Another thing I should mention is that the way you have set up your array won't work. You initialized it as an empty array and then you try to assign values to it in the for loop. It might be easier to do away with the array altogether and just use a counter in the for loop to see how many coins (either heads or tails) you get based on what the user chose. If you're really set on using arrays, you can establish a max number of coins to flip (ex: 10), then create an array of size 10 and fill in the values that way.
Finally, you should check out Random's nextInt() function for an easy way to simulate a coin flip.