Skip to content

Instantly share code, notes, and snippets.

@aindong
Created September 6, 2016 08:57
Show Gist options
  • Save aindong/ac7ad430a615e0110cd8275ee8b8a8a3 to your computer and use it in GitHub Desktop.
Save aindong/ac7ad430a615e0110cd8275ee8b8a8a3 to your computer and use it in GitHub Desktop.
import java.io.*;
import java.util.Scanner;
class GuessMyNumber {
protected int min = 1;
protected int max = 31;
public static void main(String args[])
{
GuessMyNumber instance = new GuessMyNumber();
instance.startGame();
}
public void startGame()
{
String output = "0";
int formatCounter = 0;
String input = "";
Scanner sc = new Scanner(System.in);
for (int i = 1; i <= 5; i++) {
int set = i;
for(int j = this.min; j <= this.max; j++) {
int binaryLen = Integer.toBinaryString(this.max).length();
String binary = Integer.toBinaryString(j);
String binaryFormatted = String.format("%0"+binaryLen+"d", Integer.parseInt(binary));
if (binaryFormatted.charAt(binaryLen - set) != '1') {
continue;
}
System.out.print(Integer.parseInt(binaryFormatted, 2) + " ");
formatCounter++;
if ((formatCounter % 4) == 0) {
System.out.println();
}
}
System.out.print("Is your number on this set [1/0]? ");
input = sc.nextLine() + input;
System.out.println();
}
System.out.println();
System.out.println("Here's your input: " + Integer.parseInt(input, 2));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment