import java.util.Scanner;
public class GuessBirthday {
public static void main(String[] args) {
// TODO 自动生成的方法存根
String set1=
"1 3 5 7\n"+
"9 11 13 15\n"+
"17 19 21 23\n"+
"25 27 29 31\n";
String set2=
"2 3 6 7\n"+
"10 11 14 15\n"+
"20 21 22 23\n"+
"26 27 30 31\n";
String set3=
"4 5 6 7\n"+
"12 13 14 15\n"+
"20 21 22 23\n"+
"28 29 30 31\n";
String set4=
"8 9 10 11\n"+
"12 13 14 15\n"+
"24 25 26 27\n"+
"28 29 30 31\n";
String set5=
"16 17 18 19\n"+
"20 21 22 23\n"+
"24 25 26 27\n"+
"28 29 30 31\n";
int day=0;
Scanner input=new Scanner(System.in);
System.out.print("Is your birthday in set1?\n");
System.out.println(set1);
System.out.println("\n Enter 1 for Yes and 0 for No:");
int answer=input.nextInt();
if(answer==1)
day+=1;
System.out.print("\nIs your birthday in set2?\n");
System.out.println(set2);
System.out.println("\n Enter 1 for Yes and 0 for No:");
answer=input.nextInt();
if(answer==1)
day+=2;
System.out.print("Is your birthday in set3?\n");
System.out.println(set3);
System.out.println("\n Enter 1 for Yes and 0 for No:");
answer=input.nextInt();
if(answer==1)
day+=4;
System.out.print("\nIs your birthday in set4?\n");
System.out.println(set4);
System.out.println("\n Enter 1 for Yes and 0 for No:");
answer=input.nextInt();
if(answer==1)
day+=8;
System.out.print("\nIs your birthday in set5?\n");
System.out.println(set5);
System.out.println("\n Enter 1 for Yes and 0 for No:");
answer=input.nextInt();
if(answer==1)
day+=16;
System.out.println("Your birthday is "+day+" !");
input.close();
}
}
介绍一下原理,可以看到这几个set里面的第一个数字 1 ,2 ,4, 8 ,16.如果answer是1,也就是在set里面的话,day加上的就是这几个数字。 这几个数字对应的二进制数分别是 1 , 10 , 100, 1000, 10000.
1到31的十进制数最多用5个二进制数就可以表示。假设是b5 b4 b3 b2 b1=b5 0000+b4 000+b3 00+b2 0+b1.
数字31也就是 11111=1+10+100+1000+10000=11111,等价于十进制的数字是---------> 1+2+4+8+16=31.
给出一个有对话框的版本。
import javax.swing.JOptionPane;
public class GuessBirthday {
public static void main(String[] args) {
// TODO 自动生成的方法存根
String set1=
"1 3 5 7\n"+
"9 11 13 15\n"+
"17 19 21 23\n"+
"25 27 29 31\n";
String set2=
"2 3 6 7\n"+
"10 11 14 15\n"+
"20 21 22 23\n"+
"26 27 30 31\n";
String set3=
"4 5 6 7\n"+
"12 13 14 15\n"+
"20 21 22 23\n"+
"28 29 30 31\n";
String set4=
"8 9 10 11\n"+
"12 13 14 15\n"+
"24 25 26 27\n"+
"28 29 30 31\n";
String set5=
"16 17 18 19\n"+
"20 21 22 23\n"+
"24 25 26 27\n"+
"28 29 30 31\n";
int day=0;
int answer = 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+"!");
}
}