Guess the Birthday java games

Source: Internet
Author: User

Recently saw a very interesting mini-game:

Ask a friend 5 questions to find out which day he was born in one months. Every question is asking if his birthday is one of the 5 numbers in a Collection.

These 5 collections Are:

Set1:1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31

Set2:2 3 6 7 10 11 14 15 18 19 22 23 26 27 30 31

Set3:4 5 6 7 12 13 14 15 20 21 22 23 28 29 30 31

Set4:8 9 10 11 12 13 14 15 24 25 26 27 28 29 30 31

Set5: 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

Birthdays are the sum of the first number of each collection that appears on this day, such as the birthday is 19, then it appears in collections 1, 2, and 5, the first number of three sets is 1, 2, and 16, and their sum is 19.

The implementation in Java Is:

Importjavax.swing.JOptionPane; public classguessbirthday{ public Static voidmain (string[] Args) {String Set1= "1 3 5 7\n" + "9 15\n" + "23\n" + "25 27 29 31"; String Set2= "2 3 6 7\n" + "ten 15\n" + "23\n" + "26 27 30 31"; String Set3= "4 5 6 7\n" + "15\n" + "23\n" + "28 29 30 31"; String Set4= "8 9 11\n" + "15\n" + "27\n" + "28 29 30 31"; String Set5= "19\n" + "23\n" + "27\n" + "28 29 30 31"; intDay=0; //Prompt The user to answer questions        intAnswer=joptionpane.showconfirmdialog (NULL,                   "is your birthday in these numbers?\n" +set1); if(answer==Joptionpane.yes_option) day+=1; Answer=joptionpane.showconfirmdialog (NULL,                  "is your birthday in these numbers?\n" +set2); if(answer==Joptionpane.yes_option) day+=2; Answer=joptionpane.showconfirmdialog (NULL,                   "is your birthday in these numbers?\n" +set3); if(answer==Joptionpane.yes_option) day+=4; Answer=joptionpane.showconfirmdialog (NULL,                  "is your birthday in these numbers\n" +set4); if(answer==Joptionpane.yes_option) day+=8; Answer=joptionpane.showconfirmdialog (NULL,                  "is your birthday in these numbers?\n" +set5); if(answer==Joptionpane.yes_option) day+=16; Joptionpane.showmessagedialog (NULL, "Your birthday is" + day+"!"); }  } 

To run the Test:

Test date is 19

Collection 2: Select "yes"

Collection 1: Select "yes"

Collection 3: Select No

Collection 4: Select No

Collection 5: Select "yes"

Show Birthday 19, correct!

principle, it involves some binary related knowledge.

Decimal numbers from 1 to 31 can be represented by a maximum of 5-bit binary numbers:

Decimal Binary
1 00001
2 00010
3 00011
...
19 10011
...
31 11111

Assuming that the binary number is represented as b5b4b3b2b1,

And the number of these 5 sets is a set of B1 1, B2 is a collection of 1, B3 is a set of 1, b4 is a set of 1, B5 is a collection of 1,

If the BK bit of the day is 1, then that number appears in the setk,

For Example: 19 of the binary number is 10011, so it appears in the set1,set2,set5;

The binary number of 30 is 11111, so it appears in the set1~set5.

In short, 5 sets determines whether the value on the corresponding position in the 5-bit binary number is 1 or 0, appears in the collection as 1, does not appear as 0, and finally consists of a 5-bit binary number, converted to 10 digits to get the birthday Date.

According to this method can guess the month, even the year, you can explore by themselves!

Guess the Birthday java games

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.