Java coin Flip Multiple instance _java

Source: Internet
Author: User
Tags stringbuffer

What are the deficiencies or questions I would like to be able to get a lot of corrections, thank you

Copy Code code as follows:

Import Java.util.Scanner;

/**
*
* @author CC Example 100 coins, initially all facing down, the first time all coins reversed, the second reversal position is a multiple of 2 coins,
* Third reversal of the multiples of 3,..... Execute 100 times and ask, how many coins are finally facing up?
*
* 1. The positive and negative use of the array 1, 0, 1 for the front, 0 for the opposite;
*
*
* Results of 100 coins
int[] = = 1100100001000000100000000100000000001000000000000100000000000000100000000000000001000000000000000000
Result coin about 1 nums = 10
*/
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];

/*
* Initialization
*/
for (int i = 0; i < intcoins.length; i++) {
Intcoins[i] = 0;//initialization all facing down, that is, back 0
}

Doturncoin ();

int resutltcoin = Getcoinnum ();

System.out.println ("Result coin about 1 nums =" + resutltcoin);//output end face upward

}

private static void Doturncoin () {
/*
* Flip Operation
*/
for (int i = 1; i < intcoins.length i++) {//Determine multiples

for (int j = I-1 J < Intcoins.length; J + +) {//Loop flip
If (j% i = = 0) {//coin sequence number is a multiple of the current number, flip
if (intcoins[j] = = 0) {
INTCOINS[J] = 1;
} else {
INTCOINS[J] = 0;
}
}

}
}
}

private static int Getcoinnum () {
int countnum = 0;//record number of final coin front
StringBuffer strb = new StringBuffer ();
for (int a:intcoins) {
Strb.append (A + "");
if (1 = = a)
countnum++;
}
System.out.println ("int[] = =" + STRB);//output array result
return countnum;
}

/*
* Coin number
*/
private static int Inputnum () {
System.out.println ("Input coin num:");
Scanner input = new Scanner (system.in);
return Input.nextint ();
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.