Java coin flip multiple increment trial example
- 2020-04-01 02:34:33
- OfStack
// any shortcomings or problems would be appreciated
import java.util.Scanner;
public class CoinTurn {
private static int[] intCoins;
public static void main(String[] args) {
// TODO Auto-generated method stub
int coinNum = inputNum();
intCoins = new int[coinNum];
for (int i = 0; i < intCoins.length; i++) {
intCoins[i] = 0;//Initialize all the way down, which is tails 0
}
doTurnCoin();
int resutltCoin = getCoinNum();
System.out.println("result coin about 1 nums = " + resutltCoin);//Output the final number of heads up
}
private static void doTurnCoin() {
for (int i = 1; i < intCoins.length; i++) {//Determine the multiple
for (int j = i - 1; j < intCoins.length; j++) {//Cyclic reverse
if (j % i == 0) { //If the number of COINS is a multiple of the current number, flip it
if (intCoins[j] == 0) {
intCoins[j] = 1;
} else {
intCoins[j] = 0;
}
}
}
}
}
private static int getCoinNum() {
int countNum = 0;//Record the final number of heads
StringBuffer strB = new StringBuffer();
for (int a : intCoins) {
strB.append(a + "");
if (1 == a)
countNum++;
}
System.out.println("int[] == " + strB);//Output the result of the array
return countNum;
}
private static int inputNum() {
System.out.println("input coin num: ");
Scanner input = new Scanner(System.in);
return input.nextInt();
}
}