Novice Diary-January 17
Case: Guess your birthday number
Write a program, imagine a number from 1-31 in advance, determine the number through five queries and output it.
import java.util.Scanner;
First prepare the required string
public class 猜你的生日号数 {
public static void main(String[] args) {
String set1 =
" 1 3 5 7\n" +
" 9 11 13 15\n" +
" 17 19 21 23\n" +
" 25 27 29 31";
String set2 =
" 2 3 6 7\n" +
" 10 11 14 15\n" +
" 18 19 22 23\n" +
" 26 27 30 31";
String set3 =
" 4 5 6 7\n" +
" 12 13 14 15\n" +
" 20 21 22 23\n" +
" 28 29 30 31";
String set4 =
" 8 9 10 11\n" +
" 12 13 14 15\n" +
" 24 25 26 27\n" +
" 28 29 30 31";
String set5 =
" 16 17 18 19\n" +
" 20 21 22 23\n" +
" 24 25 26 27\n" +
" 28 29 30 31";
//这些数是当它二进制位取一时的值,从倒数第一位到第五位
Main program
//定义变量day用于接收号数的值
int day = 0;
Scanner sc = new Scanner(System.in);
for (int i = 1;i <= 5;i ++){
System.out.println("你的生日号数在这个集合里面吗?\n");
if (i == 1){
System.out.println(set1);
System.out.println("\n是,请输入1;不是,请输入0");
int aaa = sc.nextInt();
day += aaa * 1;
;}
if (i == 2){
System.out.println(set2);
System.out.println("\n是,请输入1;不是,请输入0");
int aaa = sc.nextInt();
//这里涉及到位运算和二进制
day += aaa * (1 << 1);
;}
if (i == 3){
System.out.println(set3);
System.out.println("\n是,请输入1;不是,请输入0");
int aaa = sc.nextInt();
day += aaa * (1 << 2);
;}
if (i == 4){
System.out.println(set4);
System.out.println("\n是,请输入1;不是,请输入0");
int aaa = sc.nextInt();
day += aaa * (1 << 3);
;}
if (i == 5){
System.out.println(set5);
System.out.println("\n是,请输入1;不是,请输入0");
int aaa = sc.nextInt();
day += aaa * (1 << 4);
;}
}
System.out.println("\n你的生日号数是:" + day);
}
}
Effect picture,
if the number in our mind is 21